package com.sencorsta.rabs.obj.ai.api;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sencorsta.rabs.obj.ai.status.fight.BaseStatus;
import com.sencorsta.rabs.util.ResettableData;
import com.sencorsta.utils.object.Jsons;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 带复杂存储场景逻辑
 */
public class SceneWhitSubRecord extends BaseScene {
    /**
     * 子记录
     */
    @Getter
    ResettableData<BaseStatus> recordSub = new ResettableData<>(getSubType(), BaseStatus::new);

    private ResettableData.ResetType getSubType() {
        return ResettableData.ResetType.DAILY;
    }

    /**
     * 返回持久化数据
     *
     * @return 数据
     */
    @Override
    public String getData() {
        Map<String, ResettableData<BaseStatus>> dataMap = new HashMap<>();
        dataMap.put("record", record);
        dataMap.put("recordSub", recordSub);
        return Jsons.toJsonString(dataMap);
    }

    /**
     * 加载持久化数据
     */
    @Override
    public void loadData(String dataJson) {
        try {
            Map<String, ResettableData<BaseStatus>> dataMap = Jsons.fromJsonString(dataJson, new TypeReference<Map<String, ResettableData<BaseStatus>>>() {
            });
            ResettableData.ResetType newResetType = record.getResetType();
            record = dataMap.get("record");
            if (!record.getResetType().equals(newResetType)){
                record.setResetType(newResetType);
            }
            record.maybeReset();
            ResettableData.ResetType resetTypeSub = recordSub.getResetType();
            recordSub = dataMap.get("recordSub");
            if (!recordSub.getResetType().equals(resetTypeSub)){
                recordSub.setResetType(resetTypeSub);
            }
            recordSub.maybeReset();
        } catch (Exception e) {
            System.out.println(this.getClass().getSimpleName() + "检测到数据转换异常,修改过格式? = " + e);
        }
    }

    @Override
    public void forcePass() {
        getRecord().getData().setDone(true);
        getRecord().getData().setCount(99);
        getRecordSub().getData().setDone(true);
        getRecordSub().getData().setCount(99);
        getChildren().forEach(Scene::forcePass);
    }


}
