<?php

namespace Plugins\AlertPlugin;

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\AlertPlugin\Entity\AlertPluginAlert;

class AlertPlugin {

    public $Author = 'Webemyos';
    public $Version = '1.0.0.0';

    function __construct($core) {
        $this->Core = $core;
    }

    /*     * *
     * Comment Type
     */

    function GetType() {
        return "Alert";
    }

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

    function GetRegisterPlugin() {
        return "<script>document.addEventListener('DOMContentLoaded', function() { " . self::GetRegister() . "}) ;</script>";
    }

    /*     * *
     * 
     */

    function GetRegister() {
        return "Plugin.Register('AlertPlugin', 'AlertPlugin', function(){ AlertPlugin.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();
    }

    /*     * **
     * Formulair pour recevoir des alertes
     * Sur n'importe que objet
     */

    function RenderAlertForm() {
        $contactForm = new Form("alertForm", $data);

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

        $contactForm->Add(array("Type" => "TextBox",
            "Id" => "FirstName",
        ));

        $contactForm->Add(array("Type" => "TextBox",
            "Id" => "Name",
        ));

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

        $contactForm->Add(array("Type" => "Button",
            "Id" => "btnCreateAlert",
            "Value" => $this->Core->GetCode("AlertPlugin.CreateAlert"),
            "CssClass" => "btn btn-primary"
        ));

        return $contactForm->Render();
    }

    /*     * **
     * Create a alert
     */

    function CreateAlert($params) {

        $alert = new AlertPluginAlert($this->Core);
        $alert->EntityName->Value = $params->EntityName;

        if ($params->EntityId) {
            $alert->EntityId->Value = $params->EntityId;
        }

        if ($params->Email) {
            $alert->Email->Value = $params->Email;
        }
        
        if ($params->FirstName) {
            $alert->FirstName->Value = $params->FirstName;
        }

        if ($params->Name) {
            $alert->Name->Value = $params->Name;
        }
        $alert->DateCreated->Value = Date::now();
        
        if($params->Data) {
            $alert->Data->Value = json_encode(base64_decode($params->Data));
        }

        $alert->Save();
        
        return $this->Core->GetCode("AlertPlugin.AlerteCreatedSuccess");
    }

    function GetAlertByEntity($entityName, $entityId){
        
        $alerts = new AlertPluginAlert($this->Core);
        return $alerts->Find("EntityId="  . $entityId . " AND EntityName='".$entityName."'");
    }
}
