<?php

namespace GriffinForms\Frontend\Views;

class Format extends \GriffinForms\Frontend\Format
{
    protected $meta;
    protected $config;
    protected $atts;
    protected $content;
    protected $shortcode_tag;
    protected $js_files;
    protected $css_files;
    protected $item;
    
    
    protected function setCssFiles()
    {
        $this->commonCssFiles();
        $this->viewSpecificCssFiles();
    }    
    
    protected function setJsFiles()
    {
        $this->commonJsFiles();
        $this->viewSpecificJsFiles();
    }
    
    protected function commonCssFiles()
    {
        static::enqueueFormStyles($this->atts['id'] ?? 0);
    }

    public static function enqueueFormStyles(int $form_id = 0): void
    {
        $config = \GriffinForms\Config::getInstance();
        $root_url = $config->getRootUrl();

        $bootstrap_css_file         = $root_url . 'external/bootstrap/css/bootstrap.min.css';
        $inter_font_file            = $root_url . 'external/fonts/inter/inter.css';
        $material_symbols_outlined  = $root_url . 'external/google/material-symbols/css/outlined.css';
        $jquery_ui_css_file         = $root_url . 'frontend/css/jquery-ui.min.css';
        $jquery_ui_theme_file       = $root_url . 'frontend/css/jquery-ui.theme.min.css';
        $form_css_file              = $root_url . 'frontend/css/griffinforms_form.css';
        $form_css_path              = $config->getRootPath() . 'frontend/css/griffinforms_form.css';

        wp_enqueue_style('griffinforms-bootstrap-css', $bootstrap_css_file, [], '5.3.0', false);
        wp_enqueue_style('jquery-ui-css', $jquery_ui_css_file, [], '1.14.1', false);
        wp_enqueue_style('jquery-ui-theme', $jquery_ui_theme_file, [], '1.14.1', false);
        wp_enqueue_style('jquery-ui-datepicker');
        wp_enqueue_style('griffinforms-inter-font', $inter_font_file, [], '1.0', false);
        wp_enqueue_style('griffinforms-material-symbols-outlined', $material_symbols_outlined, [], '1.0', false);
        wp_enqueue_style(
            'griffinforms_form',
            $form_css_file,
            ['griffinforms-bootstrap-css'],
            file_exists($form_css_path) ? (string) filemtime($form_css_path) : '1.0'
        );

        if ($form_id > 0) {
            $sql = \GriffinForms\Frontend\Sql\Form::getInstance();
            $form_theme_id = $sql->getItemValue('form', 'id', $form_id, 'form_theme');
            if (absint($form_theme_id) < 1) {
                $no_theme_css_file = $root_url . 'frontend/css/gf-no-theme.css';
                $no_theme_css_path = $config->getRootPath() . 'frontend/css/gf-no-theme.css';
                wp_enqueue_style(
                    'griffinforms-no-theme',
                    $no_theme_css_file,
                    ['griffinforms-bootstrap-css'],
                    file_exists($no_theme_css_path) ? (string) filemtime($no_theme_css_path) : '1.0'
                );
            }
        }
    }

    protected function commonJsFiles()
    {
        static::enqueueFormAssets();
    }

    public static function enqueueFormAssets(): void
    {
        $config = \GriffinForms\Config::getInstance();
        $root_url = $config->getRootUrl();

        wp_enqueue_script('jquery');

        $bootstrap_js_file   = $root_url . 'external/bootstrap/js/bootstrap.min.js';
        $frontend_js_file    = $root_url . 'frontend/js/frontend.js';
        $locale_utils_js_file = $root_url . 'frontend/js/utils/locale.js';

        if (!wp_script_is('griffinforms-bootstrap-js', 'enqueued')) {
            wp_enqueue_script('griffinforms-bootstrap-js', $bootstrap_js_file, ['jquery'], '5.3.0', true);
        }

        if (!wp_script_is('griffinforms-locale-utils', 'enqueued')) {
            wp_enqueue_script('griffinforms-locale-utils', $locale_utils_js_file, [], '1.0.0', true);
        }

        if (!wp_script_is('jquery-ui-datepicker', 'enqueued')) {
            wp_enqueue_script('jquery-ui-datepicker');
        }

        if (!wp_script_is('griffinforms-frontend', 'enqueued')) {
            wp_enqueue_script('griffinforms-frontend', $frontend_js_file, ['jquery', 'jquery-ui-datepicker'], '1.0.0', true);
        }
    }
    
    protected function viewSpecificCssFiles()
    {
        $css_file = $this->config->getRootUrl() . '/frontend/css/' . $this->shortcode_tag . '.css';
        wp_enqueue_style($this->shortcode_tag, $css_file, [], '1.0', false);
    }
    
    protected function viewSpecificJsFiles()
    {
        $js_file = $this->config->getRootUrl() . 'frontend/js/' . $this->shortcode_tag . '.js';
        wp_enqueue_script($this->shortcode_tag, $js_file, ['jquery', 'jquery-ui-datepicker'], '1.0', false);
    }
    
    public function __construct($atts, $content, $shortcode_tag)
    {
        $this->atts = $atts;
        $this->content = $content;
        $this->shortcode_tag = $shortcode_tag;
        $this->setAll();
    }
}
