<?php

namespace Plugins\ClickTrackPlugin;

use Core\View\View;
use Core\View\ElementView;
use Core\Core\Request;
use Core\Core\Response;
use Core\Control\Form\Form;
use Core\Utility\Date\Date;

use Plugins\ClickTrackPlugin\Entity\ClickTrackPluginStat;
use Plugins\ClickTrackPlugin\Entity\ClickTrackPluginMessage;


class ClickTrackPlugin {

    public $Author = 'Webemyos';
    public $Version = '1.0.0.0';
    
    function __construct($core) {
        $this->Core = $core;
    }

    /*     * *
     * Comment Type
     */

    function GetType() {
        return "ClickTrack";
    }

    /*     * *
     * Render the Js in the VotePlugin
     */

    function GetRegisterPlugin() {
        return "<script>document.addEventListener('DOMContentLoaded', function() { " . self::GetRegister() ."}) ;</script>";
    }
    
    /***
     * 
     */
    function GetRegister(){
       return "Plugin.Register('ClickTrackPlugin', 'ClickTrackPlugin', function(){ ClickTrackPlugin.Init(); });" ;
    }
    
    /*     * *
     * Render the plugin
     */

    function Render($entityName, $entityId) {

        $view = new View(__DIR__ . "/View/index.tpl", $this->Core);
        $view->AddElement(new ElementView("EntityName", $entityName));
        $view->AddElement(new ElementView("EntityId", $entityId));
        
        
        return $view->Render();
    }
    
    /***
     * Add Stat and showx aciont
     */
    function OpenAction($params){
        
        $stat = new ClickTrackPluginStat($this->Core);
        $stat->DateCreated->Value = Date::Now();
        $stat->EntityName->Value = $params->EntityName;    
        $stat->EntityId->Value = $params->EntityId;    
        $stat->StateType->Value = $params->Action;  
        $stat->Save();
        
       switch($params->Action){
           case "phone":
               $view = new View(__DIR__ . "/View/phone.tpl", $this->Core);
               return $view->Render();
           break;
           case "contact" :
               return $this->RenderContactForm();
               break;
       }
    }
    
    
    /***
     * Formulaire de contact
     */
    function RenderContactForm(){

        $contactForm = new Form("contactForm", $data);

        $contactForm->SetView(__DIR__ . "/View/contactForm.tpl", $this->Core);
        $contactForm->SetAction("/View/contactForm.tpl");

        $contactForm->Add(array("Type" => "TextBox",
            "Id" => "FirstName",
            "Validators" => ["Required"]
        ));
        
        $contactForm->Add(array("Type" => "TextBox",
            "Id" => "Name",
            "Validators" => ["Required"]
        ));
        
        $contactForm->Add(array("Type" => "EmailBox",
            "Id" => "Email",
            "Validators" => ["Required"]
        ));

        $contactForm->Add(array("Type" => "TelBox",
            "Id" => "Phone",
            "Validators" => ["Required"]
        ));
        
         $contactForm->Add(array("Type" => "TextArea",
            "Id" => "Message",
        ));
        
        $contactForm->Add(array("Type" => "Button",
            "Id" => "btnSendContactForm",
            "Value" => $this->Core->GetCode("ClickTrackPlugin.Send"),
            "CssClass" => "btn btn-primary"
        ));

        return $contactForm->Render();
    }
    
    /****
     * Demande d'information
     */
    function SendContactForm($params){
        
        
        $message = new ClickTrackPluginMessage($this->Core);
        $message->DateCreated->Value = Date::Now();
        $message->EntityName->Value = $params->EntityName;    
        $message->EntityId->Value = $params->EntityId;    
      
        $message->FirstName->Value = $params->FirstName;    
        $message->Name->Value = $params->Name;    
        $message->Phone->Value = $params->Phone;    
        $message->Email->Value = $params->Email;    
        $message->Message->Value = $params->Message;    
      
        $message->Save();
        
        
        return $this->Core->GetCode("ClickTrackPlugin.MessageRecevied");
    }
    
    /****
     * Get Stat By Entity
     */
    function GetStatsByEntity($entityName, $entityId){
        
        $request = "select count(Id) as nbElement, StateType  FROM ClickTrackPluginStat WHERE EntityId = $entityId and EntityName= '$entityName' Group By StateType;";
        $result = $this->Core->Db->GetArray($request);
        
        return $result;
    }
    
    /***
     * Obtien tlmes message d'une annonce
     */
    function GetMessagesByEntity($entityName, $entityId){
        $messages = new ClickTrackPluginMessage($this->Core);
        
        return $messages->Find("EntityId="  . $entityId . " AND EntityName='".$entityName."'");
    }
    
}