<?php

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

namespace Apps\Base;

use Apps\Base\Helper\InstallHelper;
use Apps\Base\Module\Front\FrontController;
use Apps\Base\Module\Install\InstallController;
use Apps\Base\Module\Login\LoginController;
use Apps\Base\Module\SignUp\SignUpController;
use Core\App\Application;
use Core\Core\Core;
use Core\Core\Request;
use Core\Entity\User\User;
use Apps\Captcha\Captcha;
use Core\Utility\Email\Email;

/**
 * Base of the App
 *
 * @author jerome
 */
class Base extends Application
{
    public $Core;
    
    public $Author = 'Webemyos';
    public $Version = '2.2.2.0';
    
    /*
     * Créate de app Base
     */
    public function __construct()
    {
        $this->Core = Core::getInstance();
        parent::__construct($this->Core, "Base");
    }
    
     /**
      * Get The Public route
      */
      function GetRoute($routes = "")
      {
         if($routes == "")
         {
            $this->Route->SetPublic(array("Install", "Login", "Connect", "Disconnect", "Signup", "contact", "ForgetPassword" ,"NewPassword" , "SetLang" ,"GetDebugInfo"));
         }
         else
         {
            $this->Route->SetPublic(array_merge($routes, array("Install", "Login", "contact",  "Connect", "Disconnect", "Signup", "ForgetPassword" ,"NewPassword", "SetLang")));
         }

         return $this->Route;
      }

      /**
       * Change la langue
       */
     function SetLang($lang)
     {
        $this->Core->SetLang($lang);
        $this->Core->Redirect($this->Core->GetPath("/"));
     }

    /*
     * Get the master View
     */
    public function GetMasterView()
    {
        $frontController = new FrontController($this->Core);
        return $frontController->GetMasterView();
    }
    
    /*
     * Get the home Page
     */
    public function Index()
    {
       $this->Core->MasterView->Set("Title", $this->Core->GetCode("Home"));
       $this->Core->MasterView->Set("Description", $this->Core->GetCode("ConnetToYourEspace"));
 
        $frontController = new FrontController($this->Core);
        return $frontController->Index();
    }
    
    /*
     * Connection Module
     */
    public function Login($error ="")
    {
       $this->Core->MasterView->Set("Title", $this->Core->GetCode("Login"));
       $this->Core->MasterView->Set("Description", $this->Core->GetCode("ConnetToYourEspace"));
 
        $loginController = new LoginController($this->Core);
        return $loginController->Index($error);
    }
    
    /*
     * Connect the use
     */
    public function Connect()
    {
       $this->Core->MasterView->Set("Title", $this->Core->GetCode("Login"));
       $this->Core->MasterView->Set("Description", $this->Core->GetCode("ConnetToYourEspace"));
       
        $loginController = new LoginController($this->Core);
        return $loginController->Connect();
    }
    
    /*
     * Deconnect L'utilisateur
     */
    public function Disconnect()
    {
        $this->Core->Disconnect();
    }
    
    /*
     * Member 
     */
    public function Membre()
    {
    }
    
    /*
     * Get The contact page
     */
    public function contact()
    {
        $this->Core->MasterView->Set("Title", $this->Core->GetCode("ContactUs"));
        $valid = false;

        if(Request::IsPost())
        {
            if(!Captcha::IsValid())
            {
                $frontController = new FrontController($this->Core);
                return $frontController->Contact(false);
            }
            else
            {
                return  $this->ContactAction(Request::GetPost("tbMail"),
                                      Request::GetPost("tbName"),
                                      Request::GetPost("tbMessage")
                                     );
            }
        }
        else
        {
            $frontController = new FrontController($this->Core);
            return $frontController->Contact();
        }
    }
    
    /**
     * Send Message to Admin
     */
    public function ContactAction($email, $name, $message)
    {
        $mail = new Email($this->Core);
        $mail->Title = $this->Core->GetCode("Base.NewContactMessageTitle");
        $mail->Body = $this->Core->GetCode("Base.NewContactMessageBody");
        $mail->Body .= $email . "," .$name . ",".$message;

        $admins = $this->Core->GetAdminUser();

        foreach($admins as $admin)
        {
            $mail->Send($admin->Email->Value);
        }

        return $this->Core->GetCode("Base.ContactMessageRecevied");
    }

    /*
     * Inscription to the site
     */
    public function Signup()
    {
       $this->Core->MasterView->Set("Title", $this->Core->GetCode("Base.Register"));
       
       $singUpController = new SignUpController($this->Core);
       return $singUpController->Index();
    }

    /**
     * Password gorger
     */
    public function ForgetPassword()
    {
        $this->Core->MasterView->Set("Title", $this->Core->GetCode("Base.ForgetPassword"));
       
        $singUpController = new SignUpController($this->Core);
        return $singUpController->ForgetPassword();
    }

    /**
     * Password gorger
     */
    public function NewPassword($params)
    {
        $this->Core->MasterView->Set("Title", $this->Core->GetCode("Base.NewPassword"));
       
        $singUpController = new SignUpController($this->Core);
        return $singUpController->NewPassword($params);
    }
    /*
     * Module d'installation
     */
    public function Install()
    {
       $this->Core->MasterView->Set("Title", $this->Core->GetCode("Install"));
       $this->Core->MasterView->Set("Description", $this->Core->GetCode("InstallSite"));
   
       if(Request::IsPost())
        {
            InstallHelper::Install($this->Core,
                                   Request::GetPost("Serveur"),
                                   Request::GetPost("DataBase"),
                                   Request::GetPost("User"),
                                   Request::GetPost("Password"),
                                   Request::GetPost("Admin"),
                                   Request::GetPost("PassAdmin")
                    );
            
            $installController = new InstallController($this->Core);
            return $installController->Success();
        }
        else
        {
            $installController = new InstallController($this->Core);
            return $installController->Index();
        }
    }

    public function GetDebugInfo()
    {
       // var_dump($_SESSION["Trace"]);

        foreach($_SESSION["Trace"] as $trace => $value)
        {
            echo "<br/><br/>" . $trace . "=>" .  $value ;
           /* foreach($value as $val){
           echo "<br/><br/>" . $trace . "=>" .  $val ;
            }*/
        }
    }

    public function ClearDebuger()
    {
        unset($_SESSION["Trace"]);
    }

    /***
     * Retourne True si l'utilisateur est connecté
     */
    public function IsConnected()
    {
        if($this->Core->IsConnected()){
            return "OK";
        } else{
            return "KO";
        }
    }

    /***
     * Get list of Email can be used in Newsletter
     */
    public function GetListEmail(){
    }

    /***
     * Get list of Event can be used in WorkFlow
     */
    public function GetListEvent(){
    }
}

