<?php
	if ( ! defined( 'ABSPATH' ) ) exit;
	
    class DigitalSignage4_UpdateVideoHandlerManager
    {
        private $demande_item = null;
        private $demande_id = null;
        private $product_id = null;

        /**
         * @param bool $product_item
         * @param string $product_id
         * @param string $product_flag
         */
        public function __construct($demande_item, $demande_id, $product_id)
        {
            $this->demande_item = $demande_item;
            $this->demande_id = $demande_id;
            $this->product_id = $product_id;

            if (DigitalSignage4_Tools::isPrestashop()) {
                refresh_local_token();
            }
        }

        public function getHtmlDatas()
        {
            return $this->load();
        }

        public function echoHtmlDatas()
        {
            //die($this->getHtmlDatas());
			//die($this->load());
			return $this->load();
			//die();
        }


        private function load()
        {
            $resultHtml='';
			

            //Aller chercher le forfait
            $forfait = DigitalSignage4_ForfaitManager::instance()->getForfait();

            if (isset($this->demande_id)) {
                $video = $this->getVideo($this->demande_id);

                if (!$video) {
                    $resultHtml.= esc_html("error");
                } elseif (isset($this->demande_item) && $this->demande_item == 'true') {
                    $resultHtml.= "

                    <div style='width: 200px;margin-bottom: 10px;margin-right: 30px;background-color: rgba(62, 238, 32, 0.16);min-height: 190px;height: max-content;'>

                        <div style='margin:0; width: 100%;height: 170px' data-ds-flag='".esc_html($video->getFlag())."'
                             data-ds-id='".esc_html($video->getIdDemande())."'
                             id='video_ds_".esc_html($video->getIdDemande())."'
                             class='DigitalSignage4_creation_box DigitalSignage4_video_dispo '>";

                            

                                if ($forfait->isExpired()) {
									$resultHtml.= "<div data-ds-flag=\"error\"></div>
                                    <span class=\"info\">".esc_html(DigitalSignage4_Translate::getTraduction("Expiree"))."</span>
                                    <div class=\"DigitalSignage4_lds-ellipsis DigitalSignage4_error_video\"></div>
                                    <span> ".esc_html($video->getDateDemande())."  </span>";
									
                                } elseif ($video->getFlag() == 'error') {
									$resultHtml.= "
                                    <div data-ds-flag=\"error\"></div>
                                    <span class=\"info\">".esc_html(DigitalSignage4_Translate::getTraduction("Erreur"))."</span>
                                    <div class=\"DigitalSignage4_lds-ellipsis DigitalSignage4_error_video\"></div>
                                    <span> ".esc_html($video->getDateDemande())."  </span>";

                                } elseif ($video->getFlag() == 'waiting') {
									$resultHtml.= "
                                    <div data-ds-flag=\"waiting\"></div>
                                    <span class=\"info\">".esc_html(DigitalSignage4_Translate::getTraduction("Attente_creation"))."</span>
                                    <div class=\"sablier\"></div>
                                    <span>".esc_html($video->getDateDemande())."</span>";

								} elseif ($video->getFlag() == 'in_progress'){
									$resultHtml.= "
                                    <div data-ds-flag=\"in_progress\"></div>
                                    <span class=\"info\">".esc_html(DigitalSignage4_Translate::getTraduction("En_cours_creation"))."</span>
                                    <div class=\"DigitalSignage4_lds-ellipsis\">
                                        <div></div>
                                        <div></div>
                                        <div></div>
                                        <div></div>
                                    </div>

                                    <span> ".esc_html($video->getDateDemande())."  </span>";

                                } elseif ($video->getFlag() == 'completed'){
									$resultHtml.= "
                                    <div data-ds-flag=\"completed\"></div>
                                    <span> ".esc_html($video->getVideoName())." </span>
                                    <div class=\"video\">
										
                                        <video controls width=\"200\" height=\"114\" preload=\"metadata\" muted=\"muted\">

                                            <source src=\"".esc_url($video->getVideoUrl())."\"
                                                    type=\"video/mp4\">

                                            

                                        </video>

                                    </div>

                                    <span> ".esc_html($video->getDateDemande())."  </span>";

                                    

                                    try {
                                        $check = (DigitalSignage4_DisplayedVideoProduct::getDisplayedVideoProduct($this->product_id) == $video->getIdDemande()) ? "checked='checked'" : "";
                                    } catch (Exception $e) {
                                        $check = "";
                                    } 
									$resultHtml.= "
                                    <input onclick=\"updateDisplayedVideo(".esc_html($this->demande_id).", ".esc_html($this->product_id)." )\"
                                           type=\"radio\" name=\"displayOnFront\" ".esc_html($check)." />";


                                }
					$resultHtml.=" 
                        </div>
                    </div>";


                
                } else { 
					$resultHtml.=" 
                    <div class=\"row DigitalSignage4_row\" id=\"row_".esc_html($video->getIdDemande())."\"
                         data-id=\"".esc_html($video->getIdDemande())."\"
                         data-flag=\"".esc_html($video->getFlag())."\">

                       
                        <div class=\"cell\" data-title=\"\">";

                            if ($video->getFlag() == 'completed') {
								$resultHtml.=" 
								<button style=\"padding: .55em; border-radius: 2em; background-color: #e07281; border: none; margin-left: 1em !important;\"
                                        onclick=\"saveIdDeletePlaylist(".esc_html($video->getIdDemande()).");deletePlaylist()\"
                                        class=\"btn DigitalSignage4_button btn-primary\"
                                        data-toggle=\"modal\" data-target=\"#ModalV\">
                                    <span class=\"DigitalSignage4_material-icons\">  delete  </span>
                                </button>";
							}
						$resultHtml.=" 

                        </div>



                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Aperçu"))."\">";

                            if ($video->getFlag() == 'completed') { 
								$resultHtml.="
                                <div style=\"margin-left:10px\">
                                    <span onclick=\"toggleVideo(this)\" style=\"padding: 5px;\"
                                          class=\"DigitalSignage4_click  DigitalSignage4_material-icons\">play_circle_outline</span>
                                    <span onclick=\"toggleVideo(this , true)\"
                                          style=\" border-bottom-right-radius: 18px;border-top-left-radius: 18px;border-bottom-left-radius: 18px;padding: 5px;background: grey;color: whitesmoke\"
                                          class=\"DigitalSignage4_click  DigitalSignage4_material-icons DigitalSignage4_display_none\">highlight_off</span>
                                </div>

                                <div class=\"video\">
                                    <video controls loop=\"\"
                                           style=\"background: white; padding:10px;position: absolute;border:2px solid grey ; margin: -34px 41px;\"
                                           class=\"DigitalSignage4_display_none\" preload=\"none\" muted=\"muted\" width=\"300\">
                                        <source src=\"".esc_url($video->getVideoUrlOgv())."\"
                                                type=\"video/ogg\">
                                        <source src=\"".esc_url($video->getVideoUrl())."\"
                                                type=\"video/mp4\">
                                    </video>
                                </div>";

                            } 
						$resultHtml.="
                        </div>

                        

                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Produit"))."\">";
                            $tmp_chaine =$video->getProductId();
							
								$allowed_html = array(
									'strong' => array(),
									'a' => array(
										'href' => array(),
										'title' => array(),
										'target'=>array(),
									),
								);
								
								
								$resultHtml.=  wp_kses( $tmp_chaine, $allowed_html );
								
						$resultHtml.="		
                        </div>

                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Id"))."\"><!-- ShortCode -->";

                            

                                if ($video->getFlag() == 'error') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Erreur"));
                                } elseif ($video->getFlag() == 'waiting') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Pas_encore_dispo"));
                                } elseif ($video->getFlag() == 'in_progress') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Pas_encore_dispo"));
                                } elseif ($video->getFlag() == 'noSpace') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Secu_disque_bloque_telechargement_video"));
                                } elseif ($video->getFlag() == 'completed') {
                                    $resultHtml.= esc_html("[ProductVideoGenerator id=" . $video->getIdDemande() . "]");
                                }

                           
						$resultHtml.="
                        </div>
                        
                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Statut"))."\">";

                            

                                if ($forfait->isExpired()) {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Expiree"));
                                } elseif (substr($video->getFlag(), 0, 5) == 'error') {
                                    if (substr($video->getFlag(), 0, 6) == 'error;') {
                                        $errormsg = explode(";", $video->getFlag());
                                        $msgs = $errormsg[1];
                                        $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Erreur"));
                                        $resultHtml.= esc_html($msgs);
                                    } else {
                                        $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Erreur"));
                                    }
                                } elseif ($video->getFlag() == 'waiting') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Attente"));
                                } elseif ($video->getFlag() == 'noSpace') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Bloquee"));
                                } elseif ($video->getFlag() == 'in_progress') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("En_cours_creation"));
                                } elseif ($video->getFlag() == 'completed') {
                                    $resultHtml.= esc_html(DigitalSignage4_Translate::getTraduction("Creee"));
                                }

                            
						$resultHtml.="
                        </div>


                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Gabarit"))."\">";

                            if ($video->getGabarit()) {
								$resultHtml.="
                                <div style=\"display: flex;align-items: center;width: 100%;height: 100%\">";
                                    $resultHtml.=esc_html($video->getGabarit()->getNom())." 
									<div style=\"opacity: .5\">
                                    <span onclick=\"toggleVideo(this)\" style=\"padding: 5px;\"
                                          class=\"DigitalSignage4_click  DigitalSignage4_material-icons\">play_circle_outline</span>
                                        <span onclick=\"toggleVideo(this , true)\"
                                              style=\" border-bottom-right-radius: 18px;border-top-left-radius: 18px;border-bottom-left-radius: 18px;padding: 5px;background: grey;color: whitesmoke\"
                                              class=\"DigitalSignage4_click DigitalSignage4_material-icons DigitalSignage4_display_none\">highlight_off</span>
                                    </div>
                                    <div class=\"video\">
                                        <video controls loop
                                               style=\"background: white; padding:10px;position: absolute;border:2px solid grey ; margin: -17px 0;\"
                                               width=\"300\" class=\"DigitalSignage4_display_none\" preload=\"none\" muted=\"muted\">
                                            <source src=\"".esc_url($video->getGabarit()->getLienVideoExemple())."\">
                                                    type=\"video/mp4\">
                                        </video>
                                    </div>
                                </div>";

                            } else {
                                $resultHtml.=esc_html(DigitalSignage4_Translate::getTraduction("gabarit_introuvable"));
                            }
						$resultHtml.="
                        </div>

                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Automatique"))."\">";

                            $resultHtml.= esc_html(strtoupper(
                                ($video->getAutomatique()) ?
                                    DigitalSignage4_Translate::getTraduction("Oui") :
                                    DigitalSignage4_Translate::getTraduction("Non")
                            ));
						$resultHtml.="
                        </div>

                        <div class=\"cell\" data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Date_demande"))."\">
                            ".esc_html($video->getDateDemande())."
                        </div>

                        <div style=\"text-align: center;\" class=\"cell\"
                             data-title=\"".esc_html(DigitalSignage4_Translate::getTraduction("Download_video"))."\">";
							

                                if ($forfait->getCanDownload()) {
                                    if ($video->getFlag() == 'completed') {
										$resultHtml.="<a href=\"".esc_html($video->getVideoUrl())."\"
										   download=\"".esc_html(mb_strtolower(str_replace(" ", "_", $video->getVideoName())))."\">
										   
											<img src=\"".esc_url(DigitalSignage4_Tools::getModuleUrl() . "Files/download-ok.png")."\"/>
										</a>";
									}
                                } else {
									$trial="no";
									if ($forfait->getTrial() == true) {
										$trial="yes";
									}
									$resultHtml.="
									<div class=\"ds4_popup\" onclick=\"showHideDS4Popup(".esc_html($video->getIdDemande()).")\">
										<img src=\"".esc_url(DigitalSignage4_Tools::getModuleUrl() . "Files/download-ko.png")."\"/>
										<span class=\"ds4_popuptext\" id=\"myPopup".esc_html($video->getIdDemande())."\">".esc_html(DigitalSignage4_Translate::getTraduction("Download_ko", DigitalSignage4_Tools::getAcaciaRedirectionPageUrl("getdownload&token=".DigitalSignage4_ForfaitManager::instance()->getToken(),$trial)))."\"</span>
									</div>";
								
								} 
						$resultHtml.="
                        </div>

                    </div>";

                }
            } 

            
			
			$allowed_html = array(
				'div' => array(
					'style' => array(),
					'class' => array(),
					'role' => array(),
					'data-flag' => array(),
					'data-id' => array(),
					'data-title' => array(),
					'id' => array(),
				),
				'span' => array(
					'class' => array(),
					'style' => array(),
					'onclick' => array(),
				),
				'button' => array(
					'style' => array(),
					'class' => array(),
					'onclick' => array(),
					'data-toggle' => array(),
					'data-target' => array(),
				),
				'a' => array(
					'href' => array(),
					'target' => array(),
					'title' => array(),
					'download' => array(),
					'class' => array(),
					'style' => array(),
				),
				'img' => array(
					'src' => array(),
					'class' => array(),
					'style' => array(),
					'alt' => array(),
				),
				'video' => array(
					'controls' => array(),
					'width' => array(),
					'height' => array(),
					'preload' => array(),
					'loop' => array(),
					'muted' => array(),
					'style' => array(),
					'class' => array(),
				),
				'source' => array(
					'src' => array(),
					'type' => array(),
				),
				'input' => array(
					'type' => array(),
					'name' => array(),
					'checked' => array(),
					'onclick' => array(),
				),
			);

            //echo $oldObBufferDatas;
			
			
			
			//echo wp_kses($resultHtml, $allowed_html);
            return (wp_kses($resultHtml, $allowed_html));
			//return ($resultHtml);
        }
		
		
		

        private function getVideo($demande_id)
        {
            $sql = "SELECT * FROM ".DigitalSignage4_caller::get_video_table_name()." WHERE `id_demande` = %d";
			global $wpdb;
			$sql=$wpdb->prepare($sql,$demande_id);
				
			$video = DigitalSignage4_DemandeManager::set(DigitalSignage4_caller::getInstance())->getLocalDemandes($sql);

            if ($video) {
                $video = DigitalSignage4_Tools::convertPoductIdToLink($video);
                $video = $video[0];
                return $video;
            }

            return false;
        }
    }
