<?php

namespace Plugins\CaracteristicPlugin;

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 Core\Control\TextBox\TextBox;
use Core\Control\ListBox\ListBox;

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\CaracteristicPlugin\Entity\CaracteristicCategory;
use Plugins\CaracteristicPlugin\Entity\Caracteristic;
use Plugins\CaracteristicPlugin\Entity\CaracteristicEntity;

class CaracteristicPlugin {

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

    /*     * *
     * Comment Type
     */

    function GetType() {
        return "Caracteristic";
    }

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

    function GetRegisterPlugin() {
        return "<script>document.addEventListener('DOMContentLoaded', function() { " . self::GetRegister() ."}) ;</script>";
    }
    
    /***
     * 
     */
    function GetRegister(){
       return "Plugin.Register('CaracteristicPlugin', 'CaracteristicPlugin', function(){ CaracteristicPlugin.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\CaracteristicPlugin\Entity\CaracteristicCategory";
        $gdCategory->App = "Shop";
        $gdCategory->Action = "GetTabCategory";
  
        $gdCategory->AddArgument(new Argument("Plugins\CaracteristicPlugin\Entity\CaracteristicCategory", "AppName", EQUAL, $appName));
        
        $tbNew = new TextBox("newCaracteristicCategoryName");
        $tbNew->Style= 'width:60%;display:inline-block';
        $tbNew->PlaceHolder = $this->Core->GetCode("Base.EnterCategory");
        $view->AddElement($tbNew);

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

        $view->AddElement($buttonAdd);

        $gdCategory->AddColumn(new EntityColumn("Name", "Name"));
        $gdCategory->AddColumn(new EntityFunctionColumn("Caracteristic", "GetCaracteristics"));
        
        $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();
    }

    /***
     *  Render Member Caracteristic
     */
    function RenderMember($appName, $entityId){

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

        $view->AddElement(new ElementView("AppName", $appName));
        $view->AddElement(new ElementView("EntityId", $entityId));
        
        $lstCaracteristicCategory = new EntityListBox("lstCaracteristicCategory", $this->Core);
        $lstCaracteristicCategory->Entity = "Plugins\CaracteristicPlugin\Entity\CaracteristicCategory";
        $lstCaracteristicCategory->AddArgument(new Argument("Plugins\CaracteristicPlugin\Entity\CaracteristicCategory", "AppName", EQUAL, $appName));
        
        $lstCaracteristicCategory->ListBox->Add($this->Core->GetCode("Base.SelectACategory"), "");
        
        $view->AddElement(new ElementView("lstCaracteristicCategory", $lstCaracteristicCategory->Render()));
    
        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $CaracteristicEntity->Select("Caracteristic.Name", "CaracteristicName");
        $CaracteristicEntity->Select("CaracteristicCategory.Name", "CategoryName");
        $CaracteristicEntity->Join("Caracteristic", "Caracteristic", "left", "CaracteristicEntity.CaracteristicId = Caracteristic.Id");
        $CaracteristicEntity->Join("CaracteristicCategory", "CaracteristicCategory", "left", "Caracteristic.CategoryId = CaracteristicCategory.Id");
      
        $Caracteristics = $CaracteristicEntity->Find("EntityId=" . $entityId);

        $view->AddElement(new ElementView("Caracteristics", $Caracteristics));
        
        $view->AddElement(new ElementView("pluginScript", $this->GetRegister()));
           
        return $view->Render();
    }

    
    /***
     * Detail des compétences d'un membre
     */
    public function RenderDetailMember($entityId, $entityName = "Profil"){
         
        $view = new View(__DIR__ . "/View/detailMember.tpl", $this->Core);
        
        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $CaracteristicEntity->Select("Caracteristic.Name", "CaracteristicName");
        $CaracteristicEntity->Join("Caracteristic", "Caracteristic", "left", "CaracteristicEntity.CaracteristicId = Caracteristic.Id");
        $Caracteristics = $CaracteristicEntity->Find("EntityId=" . $entityId . " and EntityName='".$entityName."'");

        $view->AddElement(new ElementView("Caracteristics", $Caracteristics));
  
        return $view->Render();
    }

    /****
     * Show Caracteristic group by category
     */
    public function RenderDetailFront($entityId, $entityName = "Profil"){

        
        
        $categoryCaracteristic = [];

        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $CaracteristicEntity->Select("Caracteristic.Name", "CaracteristicName");
        $CaracteristicEntity->Select("CaracteristicCategory.Name", "CategoryName");
        $CaracteristicEntity->Join("Caracteristic", "Caracteristic", "left", "CaracteristicEntity.CaracteristicId = Caracteristic.Id");
        $CaracteristicEntity->Join("CaracteristicCategory", "CaracteristicCategory", "left", "Caracteristic.CategoryId = CaracteristicCategory.Id");

        $Caracteristics = $CaracteristicEntity->Find("EntityId=" . $entityId . " and EntityName='".$entityName."'");

        foreach( $Caracteristics as $caracteristic){

            $categoryCaracteristic[$caracteristic->CategoryName->Value][] = $caracteristic;
        }

        $view = "<div class='row'>";

        foreach($categoryCaracteristic as $category => $values){
          
            foreach($categoryCaracteristic[$category] as $caracteristic ){
           $view .= "<ul>";
                $view .= "<li>".$caracteristic->GetFrontTitle()."</li>";
            
         $view .= "</ul>";
                
            }
       
        }

        $view .= "</div>";
    
        return $view;

    }
    
    /***
     * Get filters 
     */
    function RenderFilters($appName){
        
        $category = new CaracteristicCategory($this->Core);
       
        $categorys = $category->Find("AppName='".$appName."'"); 

        $view = "<div class='full-first container'>";
        $view .= "<div class='col-md-11 row' id='filterContainer' >";
        foreach($categorys  as $category){

            $carateristic = new Caracteristic($this->Core);
            $carateristics = $carateristic->Find("CategoryId=" .$category->IdEntite); 
                $view .= "<div class='col-md-3'>";

            $view .= "<label>".$category->Name->Value."</label>";

            if(count($carateristics) == 0){
                $tbFilter = new TextBox($category->IdEntite);
                $tbFilter->CssClass = "form-control";
                $view .= $tbFilter->Render();
            } else{

                $tbFilter = new ListBox($category->IdEntite);
                $tbFilter->CssClass = "form-control";
                $tbFilter->Add("", "");
                
                foreach($carateristics as $caracteristic ){
                    $tbFilter->Add($caracteristic->Name->Value, $caracteristic->IdEntite);
                }
                $view .= $tbFilter->Render();
            }

            $view .= "</div>";
        }
        $view .= "</div>";
        $view .= "<div class='col-md-1'><label>&nbsp;</label><div><i id='btnFilterCaracteristic'  class='fa fa-search fa-2x cursorPointer'></i></div></div>";

        $view .= "</div>";

        return $view;
    }

    /***
     * Add Categorie of compétences
     */
    function AddCategory($params){

        $category = new CaracteristicCategory($this->Core);
        $category->Name->Value = $params->name;
        $category->AppName->Value = $params->appName;
        $category->Save();

        return $this->RenderAdmin($params->appName);
    }

    /***
     * AddCaracteristic to a category
     */
    function AddCaracteristic($params){

        $Caracteristic = new Caracteristic($this->Core);
        $Caracteristic->Name->Value = $params->name;
        $Caracteristic->CategoryId->Value = $params->categoryId;
        $Caracteristic->Save();

        $category = new CaracteristicCategory($this->Core);
        $category->GetById($params->categoryId);
        return $category->GetCaracteristics();
    }

    /**
     * Delete a Caracteristic
     */
    function RemoveCaracteristic($params){

        $Caracteristic = new Caracteristic($this->Core);
        $Caracteristic->GetById($params->id);

        $this->DeleteCaracteristicEntity($Caracteristic->IdEntite);
          
        $Caracteristic->Delete();
    }

    /**
     * Remove a category
     */
    function DeleteCategory($params){

        $category = new CaracteristicCategory($this->Core);
        $category->GetById($params->id);

        $Caracteristic = new Caracteristic($this->Core);
        $Caracteristics =  $Caracteristic->Find("CategoryId=" .$params->id );

        foreach($Caracteristics as $Caracteristic){

            $this->DeleteCaracteristicEntity($Caracteristic->IdEntite);
            $Caracteristic->Delete();
        }

        $category->Delete();

        return $this->RenderAdmin($params->appName);
    }

    /**
     * Delete user Caracteristic
     */
    function DeleteCaracteristicEntity($CaracteristicId){

        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $Caracteristics = $CaracteristicEntity->Find("CaracteristicId=" .$CaracteristicId );

        foreach($Caracteristics as $Caracteristic){
            $Caracteristic->Delete();
        }
    }
    
    /***
     * Get All Caracteristic to a category
     */
    function GetCaracteristicsByCategory($params){
      $category = new CaracteristicCategory($this->Core);
      $category->GetById($params->id);
      
      return $category->GetCaracteristics(true, false);
    }
    
    /***
     * Add A Caracteristic to a entity
     */
    function AddToEntity($params){
        
        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $CaracteristicEntity->CaracteristicId->Value = $params->id;
        $CaracteristicEntity->EntityName->Value = $params->appName;
        $CaracteristicEntity->EntityId->Value = $params->entityId;
        $CaracteristicEntity->Description->Value = $params->description;
        
        $CaracteristicEntity->Save();
        
                
        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $CaracteristicEntity->Select("Caracteristic.Name", "CaracteristicName");
        $CaracteristicEntity->Join("Caracteristic", "Caracteristic", "left", "CaracteristicEntity.CaracteristicId = Caracteristic.Id");
        $Caracteristics = $CaracteristicEntity->Find("EntityId=" . $params->entityId);
        
        $view = "";
        
        foreach($Caracteristics as $Caracteristic){
        
            $view .= "<div class='chips'>
                ".$Caracteristic->GetTitle()."
                <i class='fa fa-trash removeCaracteristicEntity' id='".$Caracteristic->IdEntite."'  ></i>
            </div>";
        }
        
        return  $view;
    }
    
    /****
     * Remove Caracteristic to a entity
     */
    function RemoveToEntity($params){
        
        $CaracteristicEntity = new CaracteristicEntity($this->Core);
        $CaracteristicEntity->GetById($params->id);
        $CaracteristicEntity->Delete();
    }
    
    function GetInfoByAppAndEntity($params){
       $entityName = Request::GetPost("EntityName");
       $entityId = Request::GetPost("EntityId");
       
       $category = new CaracteristicCategory($this->Core);
       $categorys = $category->Find("AppName='" . $entityName."'");
       
       $CaracteristicArray = array();
       
       foreach($categorys as $category){
           $Caracteristic = new Caracteristic($this->Core);
           $Caracteristics = $Caracteristic->Find("CategoryId=" . $category->IdEntite);
           
           foreach($Caracteristics as $Caracteristic){
               $CaracteristicArray[] = $Caracteristic->ToArray();
           }
       }
       
       $CaracteristicEntity = new CaracteristicEntity($this->Core);
       $CaracteristicEntity->Select("Caracteristic.Name", "CaracteristicName");
       $CaracteristicEntity->Join("Caracteristic", "Caracteristic", "left", "CaracteristicEntity.CaracteristicId = Caracteristic.Id");
       $CaracteristicEntitys = $CaracteristicEntity->Find("EntityId=" . $entityId ." AND EntityName='".$entityName."'" );

       
       $CaracteristicEntityArray = array();
       
       foreach($CaracteristicEntitys as $CaracteristicEntity){
        $CaracteristicEntityArray[] = ["Name"=> $CaracteristicEntity->CaracteristicName->Value, "IdEntite" => $CaracteristicEntity->IdEntite];
       }
       
       return json_encode(array("Categories" => $category->ToAllArray($categorys), 
                                "Caracteristics" => $CaracteristicArray,
                                "CaracteristicsEntity" => $CaracteristicEntityArray ));
    }
    
    /***
     * Add Caracteristic to a entite
     */
    function AppCaracteristicEntity(){
        
       $entityName = Request::GetPost("entityName");
       $entityId = Request::GetPost("entityId");
       $CaracteristicId =  Request::GetPost("caracteristicId");
       
       $CaracteristicEntity = new CaracteristicEntity($this->Core);
       $CaracteristicEntity->EntityName->Value = $entityName;
       $CaracteristicEntity->EntityId->Value = $entityId;
       $CaracteristicEntity->CaracteristicId->Value = $CaracteristicId;
       
       $CaracteristicEntity->Save();
       
       return $this->Core->Db->GetInsertedId();
    }
    
    function RemoveCaracteristicEntity(){
        
       $id =  Request::GetPost("id");
       $CaracteristicEntity = new CaracteristicEntity($this->Core);
       $CaracteristicEntity->GetById($id);
       $CaracteristicEntity->Delete();
       
       return Response::Success([]);
       
    }
    
    /***
     * Suppression par Entity
     */
    function DeleteByEntity($EntityName, $EntityId){
        
        $entity = new CaracteristicEntity($this->Core);
        $entitys = $entity->Find("EntityName='".$EntityName."' and EntityId= " . $EntityId);
        
        foreach($entitys as $entity){
            $entity->Delete();
        }
    }
}