<?php

class ShareItWidget extends CWidget {
    public $fontsize;
    public $title;
    public $link;

    public function init() {
        // 7 buttons = 100% / 7 = ~14.285%
        $w = "14.285%";
        
        $cs = Yii::app()->getClientScript();
        $cs->registerCssFile(AssetsManager::i()->getCoreAsset("font-awesome/css/font-awesome.min.css"));
        
        $style = <<<EOF
            .shareit_outer{
                clear:both;
                padding:5px;
            }
            .shareit{
                text-align:center;
                display:block;
                float:left;
                width:$w;
                padding:3px;
                color:white;
                font-size:$this->fontsize;
                cursor: pointer;
            }
            .shareit:hover, .shareit:focus, .shareit:active{
                color:white;
                opacity:0.9;
                text-decoration:none;
            }
            /* Button Colors */
            .facebook { background-color:#3B5B99; }
            .x-twitter { background-color:#000000; } /* X is Black */
            .whatsapp { background-color:#4BB34C; }
            .telegram { background-color:#0088cc; }
            .email { background-color:#E81A86; }
            .print { background-color:#E26900; }
            .copylink { background-color:#6c757d; }
EOF;
        $cs->registerCss("shareit", $style);
    }

    public function run() {
        // Define the array in the specific order you requested
        $links = array(
            "facebook" => array(
                "icon" => "facebook",
                "type" => "standard",
                "url_template" => "https://www.facebook.com/sharer/sharer.php?u={link}",
            ),
            "x-twitter" => array(
                "icon" => "twitter", // Uses fa-twitter (X still uses this icon code in older FA versions)
                "type" => "standard",
                "url_template" => "https://twitter.com/intent/tweet?text={title}&url={link}",
            ),
            "whatsapp" => array(
                "icon" => "whatsapp",
                "type" => "whatsapp", // Special logic
            ),
            "telegram" => array(
                "icon" => "paper-plane", // fa-paper-plane or fa-telegram depending on FA version
                "type" => "standard",
                "url_template" => "https://t.me/share/url?url={link}&text={title}",
            ),
            "email" => array(
                "icon" => "envelope",
                "type" => "email", // Special logic
            ),
            "print" => array(
                "icon" => "print",
                "type" => "print", // Special logic
            ),
            "copylink" => array(
                "icon" => "link",
                "type" => "copylink", // Special logic
            ),
        );

        echo "<div class='shareit_outer clearfix'>";

        foreach ($links as $name => $p) {
            
            // --- Logic for PRINT ---
            if ($p['type'] == "print") {
                ?>
                <a target="_blank" href="#" onclick="window.print(); return false;" class="shareit <?php echo $name; ?>" title="Print">
                    <i class="fa fa-<?php echo $p["icon"]; ?>"></i>
                </a>
                <?php
            } 
            
            // --- Logic for EMAIL ---
            else if ($p['type'] == "email") {
                ?>
                <a target="_blank" href="mailto:?Subject=<?php echo $this->title; ?>&body=<?php echo $this->link ?>" class="shareit <?php echo $name; ?>" title="Email">
                    <i class="fa fa-<?php echo $p["icon"]; ?>"></i>
                </a>
                <?php
            } 
            
            // --- Logic for WHATSAPP ---
            else if ($p['type'] == "whatsapp") {
                if (Yii::app()->mobileDetect->isMobile() || Yii::app()->mobileDetect->isTablet()) {
                    $sharing_url = "whatsapp://send";
                } else {
                    $sharing_url = "https://wa.me/";
                }
                ?>
                <a target="_blank" href="<?= $sharing_url ?>?text=<?= urlencode($this->title . " " . $this->link) ?>" class="shareit <?php echo $name; ?>" title="WhatsApp">
                    <i class="fa fa-<?php echo $p["icon"]; ?>"></i>
                </a>
                <?php
            } 
            
            // --- Logic for COPY LINK ---
            else if ($p['type'] == "copylink") {
                // Inline JS to handle copying to clipboard
                $jsOnClick = "
                    var tempInput = document.createElement('input'); 
                    tempInput.value = '" . $this->link . "'; 
                    document.body.appendChild(tempInput); 
                    tempInput.select(); 
                    document.execCommand('copy'); 
                    document.body.removeChild(tempInput); 
                    alert('Link Copied: ' + tempInput.value); 
                    return false;
                ";
                ?>
                <a href="#" onclick="<?php echo $jsOnClick; ?>" class="shareit <?php echo $name; ?>" title="Copy Link">
                    <i class="fa fa-<?php echo $p["icon"]; ?>"></i>
                </a>
                <?php
            } 
            
            // --- Logic for STANDARD LINKS (FB, X, Telegram) ---
            else {
                $url = str_replace("{title}", urlencode($this->title), $p["url_template"]);
                $url = str_replace("{link}", urlencode($this->link), $url);
                ?>
                <a target="_blank" href="<?php echo $url; ?>" class="shareit <?php echo $name; ?>" title="<?php echo ucfirst($name); ?>">
                    <i class="fa fa-<?php echo $p["icon"]; ?>"></i>
                </a>
                <?php
            }
        }
        echo "</div>";
    }
}
?>