<?php

namespace App\Helpers\Cloudflare;

use App\Helpers\CommonHelper;
use App\Models\Cloudflare\ZoneModel;

class ZoneHelper extends CommonHelper
{
    public $old_parent = "";
    public function __construct()
    {
        parent::__construct();
    }
    function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        $value = $value ?: DEFAULTS['EMPTY'];
        switch ($field) {
            case ZoneModel::PARENT:
                $extra_class = isset($extras['class']) ? $extras['class'] . ' select-field' : 'select-field';
                $form = form_dropdown($field, [
                    "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field]
                ], isset($viewDatas[$field]) ? $viewDatas[$field] : $value, ['class' => $extra_class, ...array_diff_key($extras, ['class' => ''])]);
                break;
            case ZoneModel::TITLE:
                $form = form_input($field, $value, ["placeholder" => "예)exampel.com", ...$extras]);
                break;
            case 'domains':
                $form = form_textarea($field, $value, ['rows' => '5', ...$extras]);
                break;
            case 'hosts':
                $form = form_textarea($field, $value == DEFAULTS['EMPTY'] ? "@\n*\nm\nwww" : $value, [
                    'rows' => '5',
                    ...$extras
                ]);
                break;
            case 'content':
                $form = form_input($field, $value, ["placeholder" => "예)123.123.123.123", ...$extras]);
                break;
            case "development_mode":
            case "ipv6":
            case "security_level":
            case 'status':
                $form = form_dropdown($field, [
                    "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field]
                ], $value, $extras);
                break;
            case 'type':    //A,AAA...
            case 'proxied': //CDN사용여부
                $form = form_dropdown($field, [
                    "" => lang('Cloudflare/Record.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field]
                ], $value, $extras);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    } //
    function getFieldView(string $field, array $viewDatas, array $extras = []): string
    {
        $value = $viewDatas['entity']->$field ?: DEFAULTS['EMPTY'];
        switch ($field) {
            case ZoneModel::PARENT:
                if ($this->old_parent === $viewDatas['entity']->getParent()) {
                    $value = "";
                } else {
                    $value = anchor(
                        current_url() . "/reload/" . $viewDatas['entity']->getParent(),
                        ICONS["RELOAD"],
                        [
                            "class" => "btn btn-sm btn-primary btn-circle",
                            "target" => "_self"
                        ]
                    ) . " <span class=\"label_accounts\">" .
                        preg_replace("/(\w+)@(.+)/", "$1", $viewDatas['field_options'][$field][$value])
                        . "</span>";
                }
                $this->old_parent = $viewDatas['entity']->getParent();
                break;
            case ZoneModel::TITLE:
                $value = anchor(
                    base_url() . $viewDatas['uri_path'] . 'record/reload/' . $viewDatas['entity']->getPK(),
                    ICONS["RELOAD"],
                    [
                        "class" => "btn btn-sm btn-primary btn-circle",
                        "target" => "_self"
                    ]
                ) . " " .
                    anchor(
                        base_url() . $viewDatas['uri_path'] . "record?zone_uid=" . $viewDatas['entity']->getPK(),
                        ICONS["FLAG"],
                        [
                            "class" => "btn btn-sm btn-primary btn-circle",
                            "target" => "_self"
                        ]
                    ) . " <span class=\"label_zones\">{$value}</span>";
                break;
            case 'name_servers':
            case 'original_name_servers':
                $value = str_replace(",", "<BR>", $value);
                break;
            default:
                $value = parent::getFieldView($field, $viewDatas, $extras);
                break;
        }
        return $value;
    } //
    public function getListRowColor($entity): string
    {
        return $entity->status != 'active' ? 'class="table-danger"' : "";
    }
    public function getListLabel(string $field, array $viewDatas, array $extras = []): string
    {
        $label = parent::getListLabel($field, $viewDatas, $extras);
        switch ($field) {
            case ZoneModel::TITLE:
                $label .= "<button onClick=\"getElementsByClassNameCopyToClipboard('label_zones'); return false;\" class=\"btn btn-sm btn-danger btn-circle\">Copy Domains</buttion>";
                break;
        }
        return $label;
    }
    public function getListButton(string $action, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                $pk = $viewDatas['entity']->getPK();
                $oldBatchJobUids = old("batchjob_uids", []);
                $oldBatchJobUids = is_array($oldBatchJobUids) ? $oldBatchJobUids : [$oldBatchJobUids];
                $checkbox = form_checkbox([
                    "id" => "checkbox_uid_{$pk}",
                    "name" => "batchjob_uids[]",
                    "value" => $pk,
                    "class" => "batchjobuids_checkboxs",
                    "checked" => in_array($pk, $oldBatchJobUids)
                ]);
                $extras = ["target" => "_self", ...$extras];
                $action = $checkbox . anchor(
                    current_url() . "/sync/" . $viewDatas['entity']->getPK(),
                    $viewDatas['cnt'],
                    $extras
                );
                break;
            default:
                $action = parent::getListButton($action, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
