<?php

namespace Plugins\NoResultPlugin;

use Core\View\View;
use Core\View\ElementView;

use Core\Control\TextBox\TextBox;
use Core\Control\Button\Button;
use Core\Control\EntityGrid\EntityGrid;
use Core\Control\EntityGrid\EntityColumn;
use Core\Control\EntityGrid\EntityIconColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;
use Core\Control\EntityListBox\EntityListBox;
use Core\Entity\Entity\Argument;

use Plugins\NoResultPlugin\Entity\NoResultCaracteristic;

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

    /*     * *
     * Comment Type
     */

    function GetType() {
        return "NoResult";
    }

    /*     * *
     * Render the Js in the NoResultPlugin
     */

    function GetRegisterPlugin() {
        return "<script>document.addEventListener('DOMContentLoaded', function() { " . self::GetRegister() ."}) ;</script>";
    }
    
    /***
     * 
     */
    function GetRegister(){
       return "Plugin.Register('NoResultPlugin', 'NoResultPlugin', function(){ NoResultPlugin.Init(); });" ;
    }
    
    /***
     * Render for Admin
     */
    function RenderAdmin($appName){

        $view = new View(__DIR__ . "/View/admin.tpl", $this->Core);         


        $view->AddElement(new ElementView("AppName", $appName));
        
        $gdCategory = new EntityGrid("gdCaracteristicCategory", $this->Core);
        $gdCategory->Entity = "Plugins\NoResultPlugin\Entity\NoResultCaracteristic";
        $gdCategory->App = "Shop";
        $gdCategory->Action = "GetTabCategory";
  
        $gdCategory->AddArgument(new Argument("Plugins\NoResultPlugin\Entity\NoResultCaracteristic", "AppName", EQUAL, $appName));
        
        $tbNewName = new TextBox("newCaracteristicName");
        $tbNewName->Style= 'width:60%;display:inline-block';
        $tbNewName->PlaceHolder = $this->Core->GetCode("Base.EnterName");
        $view->AddElement($tbNewName);

        $tbNewValue = new TextBox("newCaracteristicValue");
        $tbNewValue->Style= 'width:60%;display:inline-block';
        $tbNewValue->PlaceHolder = $this->Core->GetCode("Base.EnterValue");
        $view->AddElement($tbNewValue);

        $tbNewDescription = new TextBox("newCaracteristicDescription");
        $tbNewDescription->Style= 'width:60%;display:inline-block';
        $tbNewDescription->PlaceHolder = $this->Core->GetCode("Base.EnterDescription");
        $view->AddElement($tbNewDescription);

        $buttonAdd = new Button("btnAddNoResultCaracteristic");
        $buttonAdd->Id = "btnAddNoResultCaracteristic";
        $buttonAdd->Value = $this->Core->GetCode("Base.AddCaracteristic");

        $view->AddElement($buttonAdd);

        $gdCategory->AddColumn(new EntityColumn("CaracteristicName", "CaracteristicName"));
        $gdCategory->AddColumn(new EntityColumn("CaracteristicValue", "CaracteristicValue"));
        $gdCategory->AddColumn(new EntityColumn("Description", "Description"));
        
        $gdCategory->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("AddIcone", "CaracteristicPlugin.AddCaracteristic", "CaracteristicPlugin.AddCaracteristic"),
                                                        array("DeleteIcone", "CaracteristicPlugin.DeleteCategory", "CaracteristicPlugin.DeleteCategory"),
                                                  )    
                          ));
        $view->AddElement($gdCategory);

        $view->AddElement(new ElementView("pluginScript", $this->GetRegister()));

        return $view->Render();
    }

    function RenderNoResultCaracteristic(){
        return "<div id='noResultFront'></div>";

    }

    /***
     * Add nes ResultCracterisrtic
     */
    public function AddNoResultCaracteristic($params){

        $noResultCaracteristic = new NoResultCaracteristic($this->Core);
        $noResultCaracteristic->AppName->Value = $params->AppName;
        $noResultCaracteristic->CaracteristicName->Value = $params->Name;
        $noResultCaracteristic->CaracteristicValue->Value = $params->Value;
        $noResultCaracteristic->Description->Value = $params->Description;
        $noResultCaracteristic->Save();
    }
    
    /****
     * Rechecreche
     */
    public function Search($params){
        
        $search = base64_decode($params->Search);
        $params = [];
        parse_str($search, $params);
        
        foreach($params as $key => $value){
            
            if($value != ""){
                
                $category = new \Plugins\CaracteristicPlugin\Entity\CaracteristicCategory($this->Core);
                $category->GetById($key);
                
                if($category->Name->Value == "Ville"){
                    
                    $city = new \Plugins\CaracteristicPlugin\Entity\Caracteristic($this->Core);
                    $city->GetById($value);
                    
                    $cityName = $city->Name->Value;
                    
                    $cityDescription = new \Apps\estimation\Entity\estimationCity($this->Core);
                    $cityDescription = $cityDescription->GetByName($cityName);
                    
                    return $cityDescription->NoResultDescription->Value;
                }
            }
        }
    }
    
    
}