<?php

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

namespace Apps\Shop\Entity;

use Core\Entity\Entity\Entity;
use Core\Entity\Entity\Property;
use Core\Entity\Entity\EntityProperty;

use Apps\Shop\Helper\ProductHelper;


class ShopProduct extends Entity {

    const TYPE_PHYSICAL  = 1;
    const TYPE_UPLOAD = 2;
    const TYPE_SUBCRIPTION =3;
    const TYPE_RENT =3;
    

    //Constructeur
    function __construct($core) {
        //Version
        $this->Version = "2.0.0.0";

        //Nom de la table 
        $this->Core = $core;
        $this->TableName = "ShopProduct";
        $this->Alias = "ShopProduct";

        $this->ShopId = new Property("ShopId", "ShopId", NUMERICBOX, true, $this->Alias);
        $this->ParentId = new Property("ParentId", "ParentId", NUMERICBOX, false, $this->Alias);
        $this->Name = new Property("Name", "Name", TEXTBOX, true, $this->Alias);
        $this->Code = new Property("Code", "Code", TEXTBOX, true, $this->Alias);
        $this->Description = new Property("Description", "Description", TEXTAREA, true, $this->Alias);
        $this->Type = new Property("Type", "Type", TEXTBOX, false, $this->Alias);
        $this->Status = new Property("Status", "Status", NUMERICBOX, true, $this->Alias);
        $this->Price = new Property("Price", "Price", TEXTBOX, false, $this->Alias);
        $this->PriceTime = new Property("PriceTime", "PriceTime", TEXTBOX, false, $this->Alias);
        $this->PriceDelay = new Property("PriceDelay", "PriceDelay", NUMERICBOX, false, $this->Alias);
        
        //Repertoire de stockage des images    
        $this->DirectoryImage = "Data/Apps/Shop/Product/";

        $this->DirectoryDocument = "Data/Apps/Shop/ProductDocument/";

        //Creation de l entité 
        $this->Create();

    }
    
    public function GetImageMiniPath() {
         
        if (file_exists("Data/Apps/Shop/Product/" . $this->IdEntite)) {
        
            $images = $this->GetImages();
            return $images[0];
            
        } else {
            return $this->Core->GetPath("/images/nophoto.png");
        }
    }

    
    public function GetCaracteristics(){
        $caracteristiques = ProductHelper::GetCaracteristiqueByProduct($this->Core, $this->IdEntite, "entity");
        
        $view = "<ul>";
        
        foreach($caracteristiques as $caracetistique){
            $view .= "<li><b>".$caracetistique->CaracteristiqueName->Value ."</b> : " .$caracetistique->CaracteristiqueItemName->Value ."</li>";
        }
      
        $view .= "<ul>";
        
        return $view;
           
    }
    
    /****
     * Get the price
     */
    public function GetPrice(){
        
        switch($this->Type->Value){
            case self::TYPE_PHYSICAL :
                   return $this->Price->Value . "&euro;"; 
                break;
                case self::TYPE_SUBCRIPTION:
                    return $this->Price->Value . "&euro;/".$this->GetTime(); 
              
                    break;
        }
    }

    public function GetTime(){
        switch($this->PriceTime->Value){
            case 1:
                return $this->Core->GetCode("Shop.ByHour");     
            break;
            case 2:
                return $this->Core->GetCode("Shop.ByWeek");     
            break;
            case 3:
                return $this->Core->GetCode("Shop.ByMonth");     
            break;
            case 4:
                return $this->Core->GetCode("Shop.ByYear");     
            break;
        }
    }

    /***
     * Validité
     */
    public function GetValidity(){
        $price = $this->PriceDelay->Value . " ";

        switch($this->PriceTime->Value){
            case 1:
                $price .= $this->Core->GetCode("Shop.Hour");     
            break;
            case 2:
                $price .= $this->Core->GetCode("Shop.Week");     
            break;
            case 3:
                $price .= $this->Core->GetCode("Shop.Month");     
            break;
            case 4:
                $price .= $this->Core->GetCode("Shop.Year");     
            break;
        }      

        return $price;
    }

    /***
     * End of validitié 
     */
    public function GetEndValidity($dateStart){

        $date = explode("/", $dateStart);

        $dateEnd = new  \DateTime($date[2]."/" .$date[1]. "/" . $date[0] );
        $delay = $this->PriceDelay->Value; 

        switch($this->PriceTime->Value){
            case 1:
                $price .= $this->Core->GetCode("Shop.Hour");     
            
                break;
            case 2:
                $price .= $this->Core->GetCode("Shop.Week");     
            break;
            case 3:
                $dateEnd->add(new \DateInterval("P".$delay."M"));
                break;
            case 4:
                $dateEnd->add(new \DateInterval("P".$delay."Y"));
                $price .= $this->Core->GetCode("Shop.Year");     
            break;
        }

        return $dateEnd->format("d/m/y");
        return $dateStart;
    }
}

?>