<?php
class ControllerModuleFloatingContent extends Controller
{
    private $error = array();

    public function index()
    {
        $language = $this->language->load('module/floating_content');
        $this->data = array_merge($this->data, $language);

        $this->document->setTitle($this->language->get('heading_title'));

        $this->document->addScript('view/public/js/module/floating-content.js');

        $this->breadcrumbs();

        $this->load->model('setting/setting');

        if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
            $this->model_setting_setting->editSetting('floating_content', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
            $this->redirect(
                $this->url->linkAnchor(
                    'extension/module',
                    'token=' . $this->session->data['token'],
                    'tab-info=' . $this->request->get['type'],
                    'SSL'
                )
            );
        }

        $config_data = array(
            'fc_back_to_top',
            'fc_whatsapp',
            'fc_facebook',
            'fc_popup',
            'fc_exit_popup',
        );

        $this->load->type('tipocomum');
        $type_position = Type_Position::getRemoveArray(
            [Type_Position::Tp_Top, Type_Position::Tp_Bottom]
        );

        $this->data['Type_Modules_List_Banner'] = Type_ModulesListBanner::getEnumlist();

        foreach ($config_data as $conf) {
            if (isset($this->request->post[$conf])) {
                $this->data[$conf] = $this->request->post[$conf];
            } else {
                $this->data[$conf] = $this->config->get($conf);
            }

            if ($conf != 'fc_popup' && $conf != 'fc_exit_popup') {
                $this->data[$conf]['position'] = ftr_geral::getArrayTypes(
                    $type_position,
                    $this->data[$conf]['position'] ?? Type_Position::Tp_Right,
                    Type_Position::Tp_Right,
                    true
                );
            }
        }

        $config_error = array(
            'error_whatsapp_number',
            'error_facebook_page'
        );

        foreach ($config_error as $conf) {
            if (isset($this->error[$conf])) {
                $this->data[$conf] = $this->error[$conf];
            } else {
                $this->error[$conf] = '';
            }
        }


        $this->data['action'] = $this->url->link(
            'module/floating_content',
            'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'],
            'SSL'
        );

        $this->data['cancel'] = $this->url->linkAnchor(
            'extension/module',
            'token=' . $this->session->data['token'],
            'tab-info=' . $this->request->get['type'],
            'SSL'
        );


        $this->load->dmodel('tool/image');
        $this->load->library('image_validate');
        $image_validate = new Image_validate();

        $this->data['popup_no_image'] = $this->dmodel_tool_image->resize('no_image.jpg', 100, 100);
        if (
            isset($this->data['fc_popup']['image']) &&
            $this->data['fc_popup']['image'] != "" &&
            file_exists(DIR_IMAGE . $this->data['fc_popup']['image']) &&
            $image_validate->isValidate($this->data['fc_popup'], true)
        ) {
            $this->data['fc_popup']['thumb'] = $this->dmodel_tool_image->resize($this->data['fc_popup']['image'], 600, 530);
        } else {
            $this->data['fc_popup']['thumb'] = $this->dmodel_tool_image->resize('no_image.jpg', 100, 100);
        }

        if (
            isset($this->data['fc_popup']['responsive_image']) &&
            $this->data['fc_popup']['responsive_image'] != "" &&
            file_exists(DIR_IMAGE . $this->data['fc_popup']['responsive_image'])
        ) {
            $this->data['fc_popup']['responsive_thumb'] = $this->dmodel_tool_image->resize($this->data['fc_popup']['responsive_image'], 600, 530);
        } else {
            $this->data['fc_popup']['responsive_thumb'] = $this->dmodel_tool_image->resize('no_image.jpg', 100, 100);
        }


        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render('module/floating_content'));
    }


    private function breadcrumbs()
    {
        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link(
                'common/home',
                'token=' . $this->session->data['token'],
                'SSL'
            ),
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_module'),
            'href' => $this->url->linkAnchor(
                'extension/module',
                'token=' . $this->session->data['token'],
                'tab-info=' . $this->request->get['type'],
                'SSL'
            ),
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link(
                'module/certificate',
                'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'],
                'SSL'
            ),
        );
    }

    protected function validate()
    {
        if (isset($this->request->post['fc_whatsapp']['status'])) {
            $whatsapp_number = $this->request->post['fc_whatsapp']['number'];

            if (empty($whatsapp_number)) {
                $this->error['error_whatsapp_number'] = $this->language->get('error_empty_whatsapp_number');
            } elseif (strlen($whatsapp_number) < 11) {
                $this->error['error_whatsapp_number'] = $this->language->get('error_invalid_whatsapp_number');
            }
        }

        if (isset($this->request->post['fc_facebook']['status'])) {
            $facebook_page = $this->request->post['fc_facebook']['page'];

            if (empty($facebook_page)) {
                $this->error['error_facebook_page'] = $this->language->get('error_empty_facebook_page');
            }
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

}