<?php
use phpDocumentor\Descriptor\Tag\VarDescriptor;

loadController('base/module');

class ControllerDesignVisual extends ControllerBaseModule
{
    protected $referencePathFile = 'design/visual';

    protected function breadcrumbs()
    {
        $this->dataVue['breadcrumbs'] = [
            [
                'href' => $this->url->link('common/home', 'token=' . $this->token, 'SSL'),
                'text' => $this->language->get('text_home'),
            ],
            [
                'href' => $this->url->link($this->referencePathFile, 'token=' . $this->token, 'SSL'),
                'text' => $this->language->get('heading_title'),
            ]
        ];
    }

    protected function actionUrls()
    {
        $this->dataVue['action'] = $this->url->link(
            "$this->referencePathFile/save",
            'token=' . $this->token,
            'SSL'
        );

        $this->dataVue['back'] = $this->url->link(
            'common/home',
            'token=' . $this->token,
            'SSL'
        );
    }

    public function loadModel()
    {
        parent::loadModel();
        $this->load->dmodel('tool/image');
    }

    public function loadData()
    {
        parent::loadData();

        $this->dataVue['TypesForJs'] = array(
            Type_Status::getTypeForJs(),
            Type_DsimDnao::getTypeForJs(),
            Type_SearchBarForm::getTypeForJs(),
            Type_SearchBarButtonBackground::getTypeForJs(),
            Type_StatusEn::getTypeForJs(),
            Type_ProductBoxPromotionStyle::getTypeForJs(),
            Type_Tamanho::getTypeForJs(),
            Type_EfeitoImagem::getTypeForJs(),
            Type_CartIcon::getTypeForJs(),
            Type_ProductListTitleStyle::getTypeForJs(),
            Type_MenuBorderRadius::getTypeForJs(),
            Type_MenuTypography::getTypeForJs(),
            Type_ProductListTypography::getTypeForJs(),
            Type_ProductShow::getTypeForJs(),
            Type_TipoZoom::getTypeForJs(),
            Type_MenuSubCategory::getTypeForJs(),
            Type_SelectionIndicators::getTypeForJs(),
            Type_ProductListFont::getTypeForJs(),
            Type_HeaderLogoPosition::getTypeForJs(),
            Type_MenuPosition::getTypeForJs(),
            Type_MenuLinkHomeDisplay::getTypeForJs(),
            Type_gridOrListView::getTypeForJs(),
            Type_Screen::getTypeForJs(),
        );

        // o inicio("../") parte da raiz, se voltar mais pastas, não vai achar o arquivo
        $this->document->addStyle('../assets/css/visual.css');
        $this->document->addStyle('../catalog/view/theme/vista/stylesheet/itembox.css');
        $this->document->addStyle('../catalog/view/theme/vista/stylesheet/vista/default.css');

        $configs = [
            'wg24_header_btsearch',
            'wg24_icon_btnsearch_color',
            'wg24_header_bg_color',
            'wg24_heade_m_lin_bg_c',
            'wg24_color_border_background',
            'wg24_product_box_sicon_bgcolor',
            'wg24_product_box_sale_nicontcolor',
            'wg24_product_box_price_col',
            'wg24_product_box_oldprce_col',
            'wg24_product_bx_bgclr',
            'wg24_product_bx_bdrclr',
            'wg24_btn_1bg_col',
            'wg24_btn_text_col',
            'wg24_l_linkc',
            'wg24_product_box_n_col',
            'config_product_list_rating_status',
            'wg24_link_color_stick',
            'wg24_heading_transfrom',
            'wg24_l_header_color',
            'wg24_l_bodyb',
            'wg24_finalizar_carrinho',
            'wg24_finalizar_carrinho_texto',
            'wg24_mm_bgcol_center',
            'wg24_menum_lcolor',
            'wg24_mainm_lhcolor',
            'wg24_menu_hbgcolor',
            'wg24_mainm_brcol',
            'product_show_type'
        ];

        foreach ($configs as $value) {
            $this->dataVue[$value] = $this->config->get($value);
        }

        $default_lang = $this->config->get('config_language_id');

        $this->dataVue['productShowGrid'] = $this->getProductShowExample(Type_ProductShow::Grid);
        $this->dataVue['productShowSlide'] = $this->getProductShowExample(Type_ProductShow::Slide);
        $this->dataVue['productShowAsideSlide'] = $this->getProductShowExample(Type_ProductShow::AsideSlide);

        $this->dataVue['product'] = $this->getProductExample();
        $this->dataVue['departamentos'] = $this->getDepartmentsExample();
        $this->dataVue['categories'] = $this->getCategoriesExample();
        $this->dataVue['productBoxButton'] = $this->config->get('futuraquantidadeatacado_status');
        $this->dataVue['typeProductBoxButton'] = $this->load->typeForTwig('Type_ProductBoxButton');
        $this->dataVue['futura_habilita_lista_comparacao'] = ($this->config->get('wg24_futura_status_lista_comparacao') == '') || ($this->config->get('wg24_futura_status_lista_comparacao') == Type_Status::Tp_Habilitado);
        $this->dataVue['futura_habilita_lista_desejos'] = ($this->config->get('wg24_futura_status_lista_desejos') == '') || ($this->config->get('wg24_futura_status_lista_desejos') == Type_Status::Tp_Habilitado);
        $this->dataVue['novo_selo'] = 'NOVO';
        $this->dataVue['exibe_futura_preco_dinamico'] = false;
        $this->dataVue['efeitoimg'] = $this->config->get('wg24_prod_efec_img');
        $this->dataVue['title_page'] = Type_TitlePage::getNickFromValue($this->config->get('wg24_page_h_font_size'));
        $this->dataVue['logo'] = $this->dmodel_tool_image->resize($this->config->get('config_logo'), 160, 40);
        $this->dataVue['token'] = $this->token;
        $this->data['image_popup_width'] = $this->config->get('config_image_popup_width');
        $this->data['image_popup_height'] = $this->config->get('config_image_popup_height');


        if ($this->config->get('wg24_l_sale_icon_style') == 'circle') {
            $this->dataVue['itembox_sale_label'] = 'itembox-sale-label-circle';
        } elseif ($this->config->get('wg24_l_sale_icon_style') == 'square') {
            $this->dataVue['itembox_sale_label'] = 'itembox-sale-label-square';
        } else {
            $this->dataVue['itembox_sale_label'] = '';
        }


        $this->dataVue['configurations'] = $this->model_setting_setting->getSetting('visual');
        $this->dataVue['configurations']['wg24_l_tabc'] = $this->config->get('wg24_l_tabc') ? $this->config->get('wg24_l_tabc')[$default_lang] : "";
        $this->dataVue['configurations']['product_list_title_line_color'] = $this->config->get('product_list_title_line_color') ? $this->config->get('product_list_title_line_color') : '';
        $this->dataVue['configurations']['product_list_title_color'] = $this->config->get('product_list_title_color') ? $this->config->get('product_list_title_color') : '';
        $this->dataVue['configurations']['product_list_subtitle_color'] = $this->config->get('product_list_subtitle_color') ? $this->config->get('product_list_subtitle_color') : '';
    }

    protected function saveSetting()
    {
        $this->load->model('setting/setting');

        $data = array();

        if (isset($this->request->post['wg24_l_tabc'])) {
            $default_lang = $this->config->get('config_language_id');
            $this->request->post['wg24_l_tabc'] = [
                $default_lang => $this->request->post['wg24_l_tabc'],
            ];
        }

        if (isset($this->request->post['product_list_title_line_color'])) {
            $data = [
                'product_list_title_line_color' => $this->request->post['product_list_title_line_color']
            ];

            $this->model_setting_setting->editSettingForKey('colors', $data);

            unset($this->request->post['product_list_title_line_color']);
        }

        if (isset($this->request->post['product_list_title_color'])) {
            $data = [
                'product_list_title_color' => $this->request->post['product_list_title_color']
            ];

            $this->model_setting_setting->editSettingForKey('colors', $data);

            unset($this->request->post['product_list_title_color']);
        }

        if (isset($this->request->post['product_list_subtitle_color'])) {
            $data = [
                'product_list_subtitle_color' => $this->request->post['product_list_subtitle_color']
            ];

            $this->model_setting_setting->editSettingForKey('colors', $data);

            unset($this->request->post['product_list_subtitle_color']);
        }

        $this->model_setting_setting->editSetting($this->fileName, $this->request->post, 0, $this->arrayJson);
    }
    public function loadScript()
    {
        parent::loadScript();
        $this->document->addStyle('../catalog/view/theme/vista/stylesheet/nav.css');
        $this->document->addScript(
            array(
                '../catalog/view/theme/vista/plugins/photoswipe/photoswipe.min.js',
                '../catalog/view/theme/vista/plugins/photoswipe/photoswipe-ui-default.min.js',
            )
        );
        $this->document->addScript('../catalog/view/theme/vista/js/cloudzoom/jquery.elevateZoom-3.0.8.min.js');
        $this->document->addScript('view/javascript/ckeditor/ckeditor.js');
    }

    public function getProductExample()
    {
        $this->load->dmodel('tool/image');

        $aspect_ratio = str_replace(',', '.', ($this->config->get('config_image_category_height') / $this->config->get('config_image_category_width') * 100)) . '%';
        $image = $this->dmodel_tool_image->resize('data/sem-imagem.png', $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
        $image2 = $this->dmodel_tool_image->resize('data/sem-imagem.png', $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
        $thumb = $this->dmodel_tool_image->resize('data/sem-imagem.png', $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));

        return [
            'gridhave' => false,
            'product_id' => 0,
            'price_pr' => 50,
            'tabela_preco' => 0,
            'model' => '123456',
            'quantity_unit' => 1,
            'fator_multiplicador' => 0,
            'quantity_initial' => 1,
            'thumb' => $image,
            'thumb_mini' => $thumb,
            'date_added' => date('d/m/Y'),
            'porcentagemDesconto' => '-10%',
            'valida_selo' => true,
            'name' => 'PRODUTO DE EXEMPLO',
            'quantity' => 1,
            'description' => 'Produto utilizado para exemplo',
            'promotion_id' => 0,
            'promotion_started' => 0,
            'title_promotion' => 'Promoção',
            'price' => 'R$ 50,00',
            'special' => 'R$ 45,00',
            'image_extra' => $image2,
            'rating' => 5,
            'content' => false,
            'ratio' => ($aspect_ratio) ? 'style="height: 0; padding-bottom: ' . $aspect_ratio . '"' : '',
            'aviseme' => false,
            'class_one_line' => 'one-line',
            'reviews' => false,
            'href' => false,
            'product_color_description' => false,
            'notification_value' => false
        ];
    }

    public function getCategoriesExample()
    {
        $this->load->dmodel('tool/image');

        $image = $this->dmodel_tool_image->resize('data/sem-imagem.png', Type_MiniatureMedium::Tp_width, Type_MiniatureMedium::Tp_height);

        $categories = array();
        $children = [["name" => 'Filho 1'], ["name" => 'Filho 2']];
        for ($i = 1; $i <= 5; $i++) {
            $categories[] = [
                "name" => 'Exemplo ' . $i,
                "image" => $image,
                "children" => $children
            ];
        }

        return $categories;
    }

    public function getProductShowExample($type)
    {
        $this->load->dmodel('tool/image');

        $image = 'data/preview-product.png';

        $productShow = [
            'image',
            'children' => []
        ];

        if ($type == Type_ProductShow::Grid) {
            $mainImage = $this->dmodel_tool_image->resize($image, $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
            $popup = $this->dmodel_tool_image->resizeWithWatermark($image, $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));

            $productShow['image'] = $mainImage;

            for ($i = 1; $i <= 5; $i++) {
                $productShow['children'][] = $popup;
            }
        } elseif ($type == Type_ProductShow::Slide) {
            $mainImage = $this->dmodel_tool_image->resize($image, $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
            $popup = $this->dmodel_tool_image->resizeWithWatermark($image, $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));

            $productShow['image'] = $mainImage;

            for ($i = 1; $i <= 10; $i++) {
                $productShow['children'][] = $popup;
            }
        } else {
            $asideSlideWidth = 580;
            $asideSlideHeight = 730;

            $popup = $this->dmodel_tool_image->resizeWithWatermark($image, $asideSlideWidth, $asideSlideHeight);
            $productShow['image'] = $this->dmodel_tool_image->resize($image, $asideSlideWidth, $asideSlideHeight);

            for ($i = 1; $i <= 10; $i++) {
                $productShow['children'][] = $popup;
            }
        }

        return $productShow;
    }

    public function getDepartmentsExample()
    {
        $this->load->dmodel('tool/image');

        $departments = array();
        for ($i = 1; $i <= 5; $i++) {
            $departments[] = [
                "name" => 'Exemplo Departamento ' . $i,
            ];
        }

        return $departments;
    }

    protected function validate()
    {
        extract($this->request->post);
        $errors = [];
        if (isset($wg24_exibe_imagens_categorias_menu) && $wg24_exibe_imagens_categorias_menu == Type_DsimDnao::Tp_Sim) {
            $errors = $this->validate->execute([
                'wg24_largura_imagens_categorias' => [
                    'value' => $wg24_largura_imagens_categorias,
                    'validates' => [
                        'isEmpty',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => 30,
                            'max' => 360
                        ]
                    ]
                ],
                'wg24_altura_imagens_categorias' => [
                    'value' => $wg24_altura_imagens_categorias,
                    'validates' => [
                        'isEmpty',
                        [
                            'method' => 'isMinMaxNumber',
                            'min' => 30,
                            'max' => 460
                        ]
                    ]
                ],
            ]);
        }

        if (isset($wg24_l_tabc)) {
            $errors = $this->validate->execute([
                'wg24_l_tabc' => [
                    'value' => $wg24_l_tabc,
                    'validates' => [
                        [
                            'method' => 'isMinMax',
                            'min' => 0,
                            'max' => 30000
                        ]
                    ]
                ]
            ]);
        }

        return $errors;
    }
}