<?php

namespace Apps\Immo\Widget\ImmoEstimateForm;

use Core\Control\Form\Form;
use Core\View\View;
use Core\View\ElementView;

class ImmoEstimateForm extends Form {

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

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

    /****
     * Configuration pour les cms
     */
    function GetConfigCms() {

        return array("Name" => "LeadForm",
            $this->Core->GetCode("Crm.DescriptionLeadForm"),
            "Parameters" => $this->GetListType()
        );
    }

    /*     * *
     * Type of prospect
     */

    function GetListType() {

        $lstType = new ListBox("Data");
        $lstType->Add("Vendeur", 1);
        $lstType->Add("Acheteur", 2);

        return $lstType->Render();
    }

    /*     * *
     * Comment Type
     */

    function GetType() {
        return "Leads";
    }

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

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

    /*     * *
     *
     */

    function GetRegister() {
        return "Plugin.Register('ImmoLeadPlugin', 'LeadPlugin', function(){ ImmoLeadPlugin.Init(); });";
    }

    /*     * *
     * Render the LeadForm
     */

    function Render($type =1) {
        switch ($type) {

            case "1" :
                return $this->RenderSeller();
                break;
            case "2" :
                return $this->RenderBuyer();    
                break;
        }
    }

    /*     * **
     * Vendeur
     */
    function RenderSeller() {

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

        $view->AddElement(new ElementView("contactForm", $this->RenderContactForm()));
        $view->AddElement(new ElementView("calledBackForm", $this->RenderCalledBackForm()));
        
        return $view->Render();
    }

    /***
     * 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" => "Button",
            "Id" => "btnSendContactForm",
            "Value" => $this->Core->GetCode("ImmoLeadPlugin.Send"),
            "CssClass" => "btn btn-primary"
        ));

        return $contactForm->Render();
    }
    
    /***
     * Demande de rappel
     */
    function RenderCalledBackForm(){
        $contactForm = new Form("calledBackForm", $data);

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

        $contactForm->Add(array("Type" => "TelBox",
            "Id" => "Phone",
            "Validators" => ["Required"]
        ));
        
        $contactForm->Add(array("Type" => "DateBox",
            "Id" => "DateCall",
            "Validators" => ["Required"]
        ));
        
        $contactForm->Add(array("Type" => "DateTimeBox",
            "Id" => "HourCall",
            "Validators" => ["Required"]
        ));
        
        $contactForm->Add(array("Type" => "Button",
            "Id" => "btnSendCalledBackForm",
            "Value" => $this->Core->GetCode("ImmoLeadPlugin.Send"),
            "CssClass" => "btn btn-primary"
        ));

        return $contactForm->Render();
    }

    /***
     * Acheteur
     */
    function RenderBuyer(){

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

        $view->AddElement(new ElementView("contactForm", $this->RenderContactForm()));
        $view->AddElement(new ElementView("calledBackForm", $this->RenderCalledBackForm()));
        
        return $view->Render();
    }

    /**
     * Vérifies les informations saisées
     * et faite un estimation pas IA
     */
    function Estimate($params){
        
        //Clé mistral
        $apiKey = "cD60hOhJXYyWNqUlFXP0BERDzxzUCAXa";
        $mistralApi = new MistralApi($apiKey);

        $userText = $params->description;
        $result = $mistralApi->estimate($userText);

        return $result;
    }
    
    /***
     * Vérifies les informations saisées
     */
    function FindProperty($params){
        
        //Clé mistral
        $apiKey = "cD60hOhJXYyWNqUlFXP0BERDzxzUCAXa";
        $mistralApi = new MistralApi($apiKey);

        $userText = $params->description;
        $result = $mistralApi->verifySearchProperty($userText);

        return $result;
    }
    
    /****
     * Enregistrement d'un prospect
     */
    function SendContact($params){
        
        $prospect = new CrmProspect($this->Core);
        $prospect->FirstName->Value = $params->FirstName;
        $prospect->LastName->Value = $params->Name;
        $prospect->Type->Value = $params->ProspectType; 
        $prospect->Email->Value = $params->Email;
        $prospect->Phone->Value = $params->Phone;
        $prospect->Description->Value = $params->Description;
        
        $prospect->Save(); 
    }
    
    /***
     * Demande de rappe
     */
    function SendCallBackForm($params){
        
        $prospect = new CrmProspect($this->Core);
        $prospect->FirstName->Value = "Demande de rappel";
        $prospect->LastName->Value = "Demande de rappel";
        $prospect->Type->Value = $params->ProspectType; 
        $prospect->Phone->Value = $params->Phone;
        $prospect->Description->Value = $params->Description;
        
        $prospect->Save(); 
        
        $prospectId = $this->Core->Db->GetInsertedId();
        
        $calledBack = new CrmProspectCalledBack($this->Core);
        $calledBack->ProspectId->Value = $prospectId;
        $calledBack->DateCall->Value = $params->DateCall;
        $calledBack->HourCall->Value = $params->HourCall;
        $calledBack->Save();
    }

    /***
     * Picto pour 
     */
    function RenderPicto(){
        $view = new View(__DIR__ . "/View/picto.tpl", $this->Core);
  
        return $view->Render();        
    }
    
}
