<?php

namespace Application\Service\Extension\Logger;

use Application\Common\EventEnumV2;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\DataHelper;
use Application\Service\Extension\Helper\StringHelper;

abstract class BaseLogger
{
    public int $itemId = 0;
    public int $signId = 0;
    public int $checkTimeId = 0;
    public int $formId = 0;
    public int $patientId = 0;
    public string $data = '';
    public int $contentId = 0;
    public ?string $event = null;

    abstract public function flush();

    public function compareValue(array $origin, array $current)
    {
        $event = EventEnumV2::getVal(EventEnumV2::EVENT_FORM_CONTENT_EDIT);
        if (!$current) {
            $diffVal = ArrayHelper::diff($origin, $current);// array_diff_uassoc($origin, $current, $func);
        } else {
            $diffVal = ArrayHelper::diff($current, $origin);
        }

        foreach ($diffVal as $k => &$v) {
            // 非字段值就不对比了
            if ((!DataHelper::isField($k) && stripos($k, 'normal-type') === false) && !in_array($event,['FORM_CHECK_SAVE','FORM_CHECK_LOCK','FORM_CHECK_UNLOCK','FORM_CHECK_REPAIR'])) {
                unset($diffVal[$k]);
            }
            // 常规类识别 预判CS/AE 非字段值就不对比了
            if (!DataHelper::isField($k) && $event == 'FORM_CONTENT_TEMP_STORAGE' && $k != 'childData') {
                unset($diffVal[$k]);
            }
            $diffKey = $k;
            if(in_array($event,['FORM_CHECK_SAVE','FORM_CHECK_LOCK','FORM_CHECK_UNLOCK','FORM_CHECK_REPAIR'])){
                if(strpos($diffKey,'check_') !== false){
                    $diffKey = str_replace('check_','',$diffKey);
                }
                if(!in_array($diffKey,['collect_date']) && !is_numeric($diffKey)){
                    unset($diffVal[$k]);
                }
            }
            // 旧数据是array, post是string
            if (isset($origin[$k]) && !is_numeric($origin[$k])) {
                // 说明是json字符串
                $isJson = StringHelper::jsonDecode($current[$k] ?? null, false);
                if ($isJson) {
                    if (in_array($event,['FORM_CHECK_SAVE','FORM_CHECK_LOCK','FORM_CHECK_UNLOCK','FORM_CHECK_REPAIR'])) {
                        $checkDiffVal = ArrayHelper::diff($current[$k], $origin[$k]);
                        if(!empty($checkDiffVal)){
                            $originDiff = $currentDiff = [];
                            foreach ($checkDiffVal as $fieldName => $fieldValue) {
                                //if(in_array($fieldName,['id','checkname_id','item_name','unit','result','reference_range_min','min_symbol','max_symbol','reference_range_max','lock_state_small','is_normal_type'])){
                                $originDiff[$fieldName] = $origin[$k][$fieldName];
                                $currentDiff[$fieldName] = $fieldValue;
                                //}
                            }
                            $v = [
                                'origin' => $originDiff ?? [],
                                'current' => $currentDiff ?? [],
                            ];
                        }
                        continue;
                    } else {
                        $current[$k] = $isJson;
                        if ($origin[$k] === $current[$k]) {
                            unset($diffVal[$k]);
                            continue;
                        }
                    }
                }
            }

            $v = [
                'origin' => $origin[$k] ?? null,
                'current' => $current[$k] ?? null,
            ];
        }

        return $diffVal;
    }
}