<?php

/**
 * @package     Sven.Bluege
 * @subpackage  com_eventgallery
 *
 * @copyright   Copyright (C) 2005 - 2019 Sven Bluege All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');
require_once JPATH_ADMINISTRATOR . '/components/com_eventgallery/helpers/backendmedialoader.php';

// The class name must always be the same as the filename (in camel case)
class JFormFieldFoldertype extends JFormField
{

    //The field class must know its own type through the variable $type.
    protected $type = 'foldertype';

    public function getInput()
    {

        EventgalleryHelpersBackendmedialoader::load();

        /**
         * @var EventgalleryLibraryFactoryFoldertype $foldertypeFactory
         */
        $foldertypeFactory = EventgalleryLibraryFactoryFoldertype::getInstance();

        $foldertypes = $foldertypeFactory->getFolderTypes(true);

        $value = $this->form->getValue('foldertypeid');

        if ($value == null  && $foldertypeFactory->getDefaultFolderType(false) != null) {
            $value = $foldertypeFactory->getDefaultFolderType(false)->getId();
        }

        $return = Array();

        $onchange  = "let in1=document.getElementsByClassName('foldertype-input');";
        $onchange .= "for(let i=0;i<in1.length;i++) in1[i].style.display='none';";
        $onchange .= "let in2=document.getElementsByClassName('foldertype-' + this.options[this.selectedIndex].value);";
        $onchange .= "for(let i=0;i<in2.length;i++) in2[i].style.display='block';";

        $return[]  = '<select class="form-control custom-select" onchange="'. $onchange .'" name="jform[foldertypeid]" id="jform_foldertypeid">';
        foreach($foldertypes as $foldertype) {
            /**
             * @var EventgalleryLibraryFoldertype $foldertype
             */

            $value==$foldertype->getId()?$selected='selected="selected"':$selected ='';

            $return[] = '<option '.$selected.' value="'.$foldertype->getId().'">'.$foldertype->getDisplayName().'</option>';
        }
        $return[] = "</select>";

        $return[] = $this->getLocalInput();

        $return[] = $this->getPicasaInput();

        $return[] = $this->getFlickrInput();

        $return[] = $this->getS3Input();

        $return[] = $this->getGooglePhotosInput();

        $return[] = $this->getFolderInputField();

        $currentFolderType = 0;
        if (isset($value) ) {
            $currentFolderType = $value;
        }

        $return[] = "<script>";
        $return[] = "document.addEventListener('DOMContentLoaded', (event) => {";
        $return[] = "  let in1 = document.getElementsByClassName('foldertype-input');";
        $return[] = "  for(let i=0;i<in1.length;i++) in1[i].style.display = 'none';";
        $return[] = "  let in2 = document.getElementsByClassName('foldertype-$currentFolderType');";
        $return[] = "  for(let i=0;i<in2.length;i++) in2[i].style.display = 'block';";
        $return[] = "});";

        $return[] = "</script>";

        $return[] = "<hr>";

        return implode('', $return);
    }

    protected function getLocalInput() {
        $result = Array();
        $value = $this->form->getValue("folder");
        $result[] = '<div class="foldertype-0 foldertype-input">';
        $result[] = '<br>'. \Joomla\CMS\Language\Text::_('COM_EVENTGALLERY_EVENTS_SOURCE_LOCAL_FOLDER_DESC') .'<br>';
        $result[] = '<input class="form-control" onchange="document.getElementById(\'jform_folder\').value=this.value" type="text" id="foldertype-0-foldername" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" />';
        $result[] = $this->renderHelText('COM_EVENTGALLERY_EVENTS_SOURCE_LOCAL_DESC');
        $result[] = '</div>';

        return implode('', $result);
    }

    protected function getFlickrInput() {
        $result = Array();
        $value = $this->form->getValue("folder");
        $result[] = '<div class="foldertype-2 foldertype-input">';
        $result[] = '<br>'. \Joomla\CMS\Language\Text::_('COM_EVENTGALLERY_EVENTS_SOURCE_FLICKR_PHOTOSET_DESC') .'<br>';
        $result[] = '<input class="form-control" onchange="document.getElementById(\'jform_folder\').value=this.value" type="text" id="foldertype-2-photosetid" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" />';
        $result[] = $this->renderHelText('COM_EVENTGALLERY_EVENTS_SOURCE_FLICKR_DESC');
        $result[] = '</div>';
        return implode('', $result);
    }


    protected function getPicasaInput() {

        $result = Array();
        $value = $this->form->getValue("folder");
        $picasakey = $this->form->getValue("picasakey");

        $temp = explode(EventgalleryLibraryFolderPicasa::PICASA_FOLDERID_DELIMITER, $value);

        $user = implode(EventgalleryLibraryFolderPicasa::PICASA_FOLDERID_DELIMITER, array_slice($temp, 0, count($temp)-1) );

        $album = "";
        if (count($temp) > 1) {
            $album = implode(EventgalleryLibraryFolderPicasa::PICASA_FOLDERID_DELIMITER, array_slice($temp, count($temp) - 1, 1));
        }

        $onchange = "document.getElementById('jform_folder').value = document.getElementById('foldertype-1-user').value + '@' + document.getElementById('foldertype-1-album').value;";
        $result[] = '<div class="foldertype-1 foldertype-input">';
        $result[] = "<br>User<br>";
        $result[] = '<input class="form-control" onchange="'. $onchange. '" type="text" id="foldertype-1-user" value="'.$user.'" />';
        $result[] = "<br>Album<br>";
        $result[] = '<input class="form-control" onchange="'. $onchange. '" type="text" id="foldertype-1-album" value="'.$album.'" />';
        $result[] = '<br>'. \Joomla\CMS\Language\Text::_('COM_EVENTGALLERY_EVENTS_SOURCE_PICASA_KEY') .'<br>';
        $result[] = '<input class="form-control" onchange="document.getElementById(\'jform_picasakey\').value=this.value" type="text" id="foldertype-1-picasakey" value="'.$picasakey.'" />';
        $result[] = $this->renderHelText('COM_EVENTGALLERY_EVENTS_SOURCE_PICASA_KEY_DESCRIPTION');
        $result[] = "<br>";
        $result[] = "<br>";
        $result[] = '</div>';

        $picasakey = $this->form->getValue('picasakey');
        $result[] = '<input type="hidden" name="jform[picasakey]" id="jform_picasakey" value="'.$picasakey.'">';

        return implode('', $result);


    }

    protected function getS3Input() {
        $result = Array();
        $value = $this->form->getValue("folder");
        $result[] = '<div class="foldertype-3 foldertype-input">';
        $result[] = '<br>'. \Joomla\CMS\Language\Text::_('COM_EVENTGALLERY_EVENTS_SOURCE_S3_FOLDER_DESC') .'<br>';
        $result[] = '<input class="form-control" onchange="document.getElementById(\'jform_folder\').value=this.value" type="text" id="foldertype-3-foldername" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" />';
        $result[] = $this->renderHelText('COM_EVENTGALLERY_EVENTS_SOURCE_S3_DESC');
        $result[] = '</div>';

        return implode('', $result);
    }

    protected function getGooglePhotosInput() {

        $result = Array();
        $value = $this->form->getValue("folder");
        $accountid = $this->form->getValue("googlephotosaccountid");
        $album = $value;
        $title = $this->form->getValue("googlephotostitle");

        /**
         * @var EventgalleryLibraryFactoryGooglephotosaccount $accountFactory
         *
         */
        $accountFactory = EventgalleryLibraryFactoryGooglephotosaccount::getInstance();
        $accounts = $accountFactory->getUsableGooglePhotosAccounts();

        $onchange = "document.getElementById('jform_folder').value = document.getElementById('foldertype-4-album').value;";
        $onchange.= "document.getElementById('jform_googlephotosaccountid').value=document.getElementById('foldertype_4_account').options[document.getElementById('foldertype_4_account').selectedIndex].value;";
        $onchange.= "document.getElementById('jform_googlephotostitle').value=document.getElementById('foldertype-4-title').value;";

        $result[] = '<div class="foldertype-4 foldertype-input">';

        if (count($accounts) == 0) {
            $result[] = '<br><br><strong>' . JText::_('COM_EVENTGALLERY_OPTIONS_COMMON_GOOGLE_PHOTOS_API_WARNING') . '</strong>';
            $result[] = '<br><br><a href="'. JRoute::_('index.php?option=com_eventgallery&view=googlephotosaccounts') .'">'.JText::_('COM_EVENTGALLERY_GOOGLEPHOTOSACCOUNTS').'</a>';
        } else {

            $result[] = "<br><label for='foldertype_4_account'>".JText::_('COM_EVENTGALLERY_FIELD_FOLDERTYPE_GOOGLEPHOTOS_ACCOUNT')."</label><br>";
            $result[] = '<select class="form-control custom-select" id="foldertype_4_account" name="foldertype_4_account" onchange="' . $onchange . '">';
            foreach($accounts as $account) {
                /**
                 * @var EventgalleryLibraryGooglephotosaccount $account
                 */

                 $result[] = '<option '. ($accountid == $account->getId()?'selected':'') .' value="'.$account->getId().'">'.$account->getName().'</option>';

            }
            $result[] = "</select>";
            $result[] = "<br><br>".JText::_('COM_EVENTGALLERY_FIELD_FOLDERTYPE_GOOGLEPHOTOS_ALBUM')."<br>";
            $result[] = '<input class="form-control" onchange="' . $onchange . '" type="text" id="foldertype-4-album" value="' . $album . '" />';
            $result[] = "<br>";
            $result[] = "<br>";
            $result[] = JText::_('COM_EVENTGALLERY_FIELD_FOLDERTYPE_GOOGLEPHOTOS_ALBUMNAME')."<br>";
            $result[] = '<input class="form-control" disabled="true" onchange="' . $onchange . '"type="text" id="foldertype-4-title" value="' . $title . '" />';
            $result[] = "<br>";
            $result[] = "<br>";



            $result[] = JHtml::_(
                'bootstrap.renderModal',
                'google-photos-album-selector-modal',
                array(
                    'title' => JText::_('COM_EVENTGALLERY_EVENT_PICASA_ABLUM_SELECTOR'),
                    'url' => JRoute::_('index.php?option=com_eventgallery&view=googlephotos&layout=albumselector&tmpl=component'),
                    'bodyHeight' => '80',
                    'modalWidth' => '80',
                )
            );

            $result[] = '<span data-bs-toggle="modal" data-bs-target="#google-photos-album-selector-modal" data-toggle="modal" data-target="#google-photos-album-selector-modal" class="btn btn-primary">' . JText::_('COM_EVENTGALLERY_EVENT_PICASA_ABLUM_SELECTOR') . '</span>';
            $result[] = '<div id="foldertype-4-albumselectoriframe" ></div>';
            $googlephotosaccountid = $this->form->getValue('googlephotosaccountid');
            $googlephotostitle = $this->form->getValue('googlephotostitle');
            $result[] = '<input type="hidden" name="jform[googlephotosaccountid]" id="jform_googlephotosaccountid" value="'.$googlephotosaccountid.'">';
            $result[] = '<input type="hidden" name="jform[googlephotostitle]" id="jform_googlephotostitle" value="'.$googlephotostitle.'">';
        }

        $result[] = "<br>";
        $result[] = '</div>';


        return implode('', $result);


    }

    function getFolderInputField() {
        $result = [];
        $value = $this->form->getValue("folder");
        $result[] = '<input type="hidden" class="required" name="jform[folder]" id="jform_folder" value="'.$value.'">';
        return implode('', $result);

    }

    function renderHelText($key) {
        $result[] = '<small style="display:block" class="form-text text-muted">';
        $result[] = \Joomla\CMS\Language\Text::_($key);
        $result[] = '</small>';
        return implode('', $result);
    }

}
