<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Apps\Immo;

use Core\Core\Core;
use Core\Core\Request;
use Core\Core\Response;
use Apps\Base\Base;
use Apps\Immo\Module\Admin\AdminController;
use Apps\Immo\Module\Front\FrontController;
use Apps\Immo\Module\Member\MemberController;
use Apps\Immo\Helper\EstimateHelper;
use Apps\Immo\Helper\SitemapHelper;

class Immo extends Base {

    /**
     * Author and version
     * */
    public $Author = 'Webemyos';
    public $Version = '1.2.0';

    /**
     * Constructeur
     * */
    function __construct() {
        $this->Core = Core::getInstance();
        parent::__construct($this->Core, "Immo");
    }

    /**
     * Set public route
     */
    function GetRoute($routes = "") {
        $this->Route->SetPublic(array("Estimate",
            "estimation/ville/" => "EstimationCity",
            "immobilier/ville/" => "ImmobilierCity"
        ));

        return $this->Route;
    }

    /*     * *
     * Page estimation sur une ville particulier
     */

    function EstimationCity($params) {
        $frontController = new FrontController($this->Core);
        return $frontController->EstimationCity($params);
    }

    /*     * *
     * Page immo on a city
     */

    function ImmobilierCity($params) {
        $frontController = new FrontController($this->Core);
        return $frontController->ImmobilierCity($params);
    }

    /*     * *
     * Execute action after install
     */

    function PostInstall() {
        \Apps\EeApp\Helper\AppHelper::UpdateAppParams($this->Core,
                "Immo",
                $this->Version,
                "", // Add Description of the app
                0, // Set 1 if the App have a AdminWidgetDashBoard
                0  // Set 1 if the App have a Member Module
        );
    }

    /**
     * Start Admin
     */
    function Run() {
        echo parent::RunApp($this->Core, "Immo", "Immo");
    }

    /*     * **
     * Estimation rapide 
     */

    function Estimate() {
        $result = EstimateHelper::Estimate(Request::GetPost("Estimate"));

        //State de recherche
        $stateApp = \Core\Dashboard\DashBoardManager::GetApp("Stat", $this->Core);

        if ($stateApp) {
            $stateApp->AddStat("Immo.Estimate", "Front", json_encode(["search" => Request::GetPost("Estimate"),
                "result" => $result]));
        }

        return $result;
    }

    /*     * *
     * Save une ville
     */

    function SaveCity() {

        $adminController = new AdminController($this->Core);

        if ($adminController->SaveCity($this->Core, Request::GetPosts())) {
            return Response::Success($adminController->GetTabCity());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Save un project
     */

    function SaveProject() {

        $adminController = new AdminController($this->Core);

        if ($adminController->SaveProject(Request::GetPosts())) {
            return Response::Success($adminController->GetTabProject());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Remove a user to a project
     */

    function RemoveUserProjet() {

        $adminController = new AdminController($this->Core);

        if ($adminController->RemoveUserProjet(Request::GetPost("UserProjetId"))) {
            return Response::Success([]);
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Sauvegarde l'event du project
     */

    function SaveEventProject() {
        $adminController = new AdminController($this->Core);

        if ($adminController->SaveEventProject(Request::GetPosts())) {
            return Response::Success($adminController->GetTabProject());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Remove event to a project
     */

    function RemoveEventProject() {
        $adminController = new AdminController($this->Core);

        if ($adminController->RemoveEventProject(Request::GetPost("EventId"))) {
            return Response::Success([]);
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Ajotue une annonce au projet
     */

    function AddAnnonceToProjet() {
        $adminController = new AdminController($this->Core);

        if ($adminController->AddAnnonceToProjet(Request::GetPosts())) {
            return Response::Success([]);
        } else {
            return Response::Error();
        }
    }

    /***
     * Delete a annonce
     */
    function RemoveAnnonce(){
        $adminController = new AdminController($this->Core);

        if ($adminController->RemoveAnnonce(Request::GetPost("annonceId"))) {
            return Response::Success([]);
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Run member Controller
     * 
     */

    function RunMember() {
        $memberController = new MemberController($this->Core);
        return $memberController->Index();
    }

   /**
     * Get The siteMap 
     */
     public function GetSiteMap($returnUrl = false){
        return SitemapHelper::GetSiteMap($this->Core, $returnUrl);
     }

    /*     * *
     * Get the widget
     */
    public function GetWidget($type = "", $params = "") {

        switch ($type) {
            case "AdminDashboard" :
                $widget = new \Apps\Immo\Widget\AdminDashBoard\AdminDashBoard($this->Core);
                break;
            case "Estimate" :
                $widget = new \Apps\Immo\Widget\ImmoEstimateForm\ImmoEstimateForm($this->Core);
                break;
        }

        return $widget->Render();
    }

    /*     * *
     * Get Forum Addable widget
     */

    public function GetListWidget() {
        return array(array("Name" => "Estimate",
                "Description" => $this->Core->GetCode("Immo.DescriptionEstimate")),
        );
    }
}
