<?php

namespace BitApps\BTCBI_PRO\Triggers\Divi;

use BitApps\BTCBI_PRO\Core\Util\Helper;
use BitApps\BTCBI_PRO\Triggers\TriggerController;
use BitCode\FI\Flow\Flow;

final class DiviController
{
    public static function info()
    {
        $plugin_path = 'Divi/index.php';

        return [
            'name'                => 'Divi',
            'title'               => 'Divi isn\'t just a WordPress theme, it\'s a complete design framework that allows you to design and customize every part of your website from the ground up.',
            'slug'                => $plugin_path,
            'pro'                 => $plugin_path,
            'type'                => 'custom_form_submission',
            'is_active'           => self::is_divi_active(),
            'activation_url'      => wp_nonce_url(self_admin_url('plugins.php?action=activate&amp;plugin=' . $plugin_path . '&amp;plugin_status=all&amp;paged=1&amp;s'), 'activate-plugin_' . $plugin_path),
            'install_url'         => wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $plugin_path), 'install-plugin_' . $plugin_path),
            'documentation_url'   => 'https://bitapps.pro/docs/bit-integrations/trigger/divi-integrations/',
            'tutorial_url'        => 'https://youtube.com/playlist?list=PL7c6CDwwm-AJpOfpcioYpZOYbhcY_qTLN&si=PnquORcc8830jEg3',
            'triggered_entity_id' => 'et_pb_contact_form_submit',
            'fetch'               => [
                'action' => 'divi/test',
                'method' => 'post',
            ],
            'fetch_remove' => [
                'action' => 'divi/test/remove',
                'method' => 'post',
            ],
            'note'  => '<p>The <b>Divi Email Optin Form Module</b> does not work with <b>Bit Integrations</b>. Only the <b>Divi Contact Form Module</b> is compatible and works well with <b>Bit Integrations</b>.</p>',
            'isPro' => true
        ];
    }

    public static function is_divi_active()
    {
        global $themename;
        if (empty($themename)) {
            return false;
        }

        $diviThemes = [
            'divi',
            'extra',
            'bloom',
            'monarch',
        ];

        return \in_array(strtolower($themename), $diviThemes);
    }

    public function getTestData()
    {
        return TriggerController::getTestData('divi');
    }

    public function removeTestData($data)
    {
        return TriggerController::removeTestData($data, 'divi');
    }

    public static function handle_divi_submit($et_pb_contact_form_submit, $et_contact_error, $contact_form_info)
    {
        $recordData = DiviHelper::extractRecordData($contact_form_info, $et_pb_contact_form_submit);
        $formData = DiviHelper::setFields($recordData);
        $reOrganizeId = $contact_form_info['contact_form_unique_id'] . '_' . $contact_form_info['contact_form_number'];

        if (get_option('btcbi_divi_test') !== false) {
            update_option('btcbi_divi_test', [
                'formData'   => $formData,
                'primaryKey' => [(object) ['key' => 'id', 'value' => $recordData['id']]]
            ]);
        }

        $flows = DiviHelper::fetchFlows($recordData['id'], $reOrganizeId);
        if (!$flows) {
            return;
        }

        foreach ($flows as $flow) {
            $flowDetails = Helper::parseFlowDetails($flow->flow_details);

            if (!isset($flowDetails->primaryKey) && ($flow->triggered_entity_id == $recordData['id'] || $flow->triggered_entity_id == $reOrganizeId)) {
                $data = DiviHelper::prepareDataForFlow($et_pb_contact_form_submit);
                Flow::execute('Divi', $flow->triggered_entity_id, $data, [$flow]);

                continue;
            }

            if (\is_array($flowDetails->primaryKey) && DiviHelper::isPrimaryKeysMatch($recordData, $flowDetails)) {
                $data = array_column($formData, 'value', 'name');
                Flow::execute('Divi', $flow->triggered_entity_id, $data, [$flow]);
            }
        }

        return ['type' => 'success'];
    }
}
