<?php

namespace App\Helpers\Cloudflare;

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

class RecordHelper extends CommonHelper
{
    public $old_parent = "";
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        $value = $value ?: DEFAULTS['EMPTY'];
        switch ($field) {
            case RecordModel::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 RecordModel::TITLE: //host
                $form = form_input($field, $value, $extras);
                break;
            case 'hosts':
                $form = form_textarea($field, html_entity_decode($value), [
                    'rows' => '5',
                    ...$extras
                ]);
                break;
            case 'content':
                $form = form_input($field, $value, ["placeholder" => "예)123.123.123.123", ...$extras]);
                break;
            case "type":
            case "fixed":
            case "locked":
            case "proxied":
            case "proxiable":
                $form = form_dropdown($field, [
                    "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field],
                ], $value, $extras);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    } //
    public function getFieldView(string $field, array $viewDatas, array $extras = []): string
    {
        $value = $viewDatas['entity']->$field ?: DEFAULTS['EMPTY'];
        switch ($field) {
            case RecordModel::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_zones\">{$viewDatas['field_options'][$field][$value]}</span>";
                }
                $this->old_parent = $viewDatas['entity']->getParent();
                break;
            case RecordModel::TITLE:
                $url = sprintf("%s/toggle/%s/fixed?fixed=%s", current_url(), $viewDatas['entity']->getPK(), $viewDatas['entity']->fixed == 'on' ? "off" : "on");
                $value = sprintf("%s%s", $viewDatas['entity']->fixed == 'on' ? "<span class=\"text-danger\">" . ICONS['LOCK'] . "</span>" : "", $value);
                $value = anchor($url, $value, ["target" => "_self"]);
                break;
            default:
                $value = parent::getFieldView($field, $viewDatas, $extras);
                break;
        }
        return $value;
    } //

    public function getListRowColor($entity): string
    {
        return $entity->locked != 'on' ? 'class="table-danger"' : "";
    }
    public function getListLabel(string $field, array $viewDatas, array $extras = []): string
    {
        $label = parent::getListLabel($field, $viewDatas, $extras);
        switch ($field) {
            case RecordModel::PARENT:
                $label .= "<button onClick=\"getElementsByClassNameCopyToClipboard('label_zones'); return false;\" class=\"btn btn-sm btn-danger btn-circle\">Copy Domains</buttion>";
                break;
            case RecordModel::TITLE:
                $label .= "<button onClick=\"getElementsByClassNameCopyToClipboard('label_hosts'); return false;\" class=\"btn btn-sm btn-danger btn-circle\">Copy Hosts</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;
    }
}
