<?php

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

namespace Apps\Shop\Module\Download;

use Core\Control\Button\Button;
use Core\Controller\Controller;
use Core\View\View;
use Core\Utility\Date\Date;

use Apps\Shop\Entity\ShopProduct;
use Apps\Shop\Entity\ShopProductDownloaded;
use Apps\Shop\Widget\DonwloadContactForm\DonwloadContactForm;

/*
 * 
 */
 class DownloadController extends Controller
 {
    /**
     * Constructeur
     */
    function _construct($core="")
    {
          $this->Core = $core;
    }

    /**
     * Create
     */
    function Create()
    {
    }

    /**
     * Init
     */
    function Init()
    {
    }

    /**
     * Render
     */
    function Show($all=true)
    {
         return $this->Index();
    }
    
    /*
    * Get the home page
    */
   function Index()
   {
       $view = new View(__DIR__."/View/index.tpl", $this->Core);
       return $view->Render();
   }
   
   /***
    * Download paeg with Email
    */
   function DownLoad($token){
   
        $donwloadContactForm = new DonwloadContactForm($this->Core, $token);
        return $donwloadContactForm->Render();
   } 

   /***
    * Save a contact and Force Donwload
    */
   function SaveContactAndDownload($data){
     
    $productId = base64_decode($data["Token"]);

    $product = new ShopProduct($this->Core);
    $product->GetById($productId);
    
    if($product){
        $productDownload = new ShopProductDownloaded($this->Core);
        $productDownload->DateDownload->Value = Date::Now();

        $productDownload->UserId->Value = "0";

        $productDownload->ProductId->Value = $product->IdEntite;

        $productDownload->Data->Value = json_encode($data);
        $productDownload->Save();

        return true;
    }
   }

   /***
    * Téléchargement d'un produit
    */
   function DownLoadFree($token){
        $productId = base64_decode($token);

        $product = new ShopProduct($this->Core);
        $product->GetById($productId);

        //TODO VERIFIER LE PRIX ET SI PAS GRATUIT VERIFIER LA COMMANDE

        if($product){

            //Save the stat Upload
            $productDownload = new ShopProductDownloaded($this->Core);
            $productDownload->DateDownload->Value = Date::Now();

            if($this->Core->isConnected())
            {
                $productDownload->UserId->Value = $this->Core->User->IdEntite;
            } 

            $data = ["product" => $product->Name->Value];

            $productDownload->Data->Value = json_encode($data);

            $productDownload->ProductId->Value = $product->IdEntite;
            $productDownload->Save();


            $document = $product->GetDocuments();
            $document = $document[0];
            $info = explode("/", $document);
            $fileName =  $info[count($info) - 1];   
         
           
            header("Content-type:application/octet-stream");
            header("Content-disposition:attachment;filename=".$fileName);

            readfile($document);
            ob_flush();
            
        } else{

            $this->Core->Redirect("/Home");    
            //Echo "Peoruit Incoonu"
        }
   }

          
          /*action*/
 }?>