package cn.stylefeng.team.meta.modular.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.dict.api.enums.DictTypeClassEnum;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.pinyin.api.PinYinApi;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.team.meta.modular.entity.MetaUserDict;
import cn.stylefeng.team.meta.modular.pojo.SysDictAggregation;
import cn.stylefeng.team.meta.modular.pojo.metadict.MetaUserDictItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MetaDictFactory {

    public static List<SysDictAggregation> createSysDictInfo(List<MetaUserDict> metaUserDictList) {

        List<SysDictAggregation> sysDictAggregations = new ArrayList<>();
        if (ObjectUtil.isEmpty(metaUserDictList)) {
            return sysDictAggregations;
        }

        for (MetaUserDict metaUserDict : metaUserDictList) {
            SysDictAggregation sysDictAggregation = new SysDictAggregation();

            SysDictType sysDictType = createSysDictType(metaUserDict);
            sysDictAggregation.setSysDictType(sysDictType);

            List<SysDict> sysDictList = createUserDictList(metaUserDict);
            sysDictAggregation.setSysDictList(sysDictList);

            sysDictAggregations.add(sysDictAggregation);
        }

        return sysDictAggregations;
    }

    private static List<SysDict> createUserDictList(MetaUserDict metaUserDict) {
        List<SysDict> sysDictList = new ArrayList<>();
        List<Map<String, Object>> userDictList = metaUserDict.getDictList();
        if (ObjectUtil.isEmpty(userDictList)) {
            return sysDictList;
        }
        for (Map<String, Object> map : userDictList) {

            MetaUserDictItem metaUserDictItem = BeanUtil.mapToBean(map, MetaUserDictItem.class, true, CopyOptions.create().ignoreError());

            SysDict sysDict = new SysDict();
            sysDict.setDictId(metaUserDictItem.getDictId());
            sysDict.setDictTypeId(metaUserDict.getMetaUserDictId());
            sysDict.setDictCode(metaUserDictItem.getDictCode());
            sysDict.setDictName(metaUserDictItem.getDictName());

            PinYinApi pinYinApi = SpringUtil.getBean(PinYinApi.class);
            sysDict.setDictNamePinyin(pinYinApi.parsePinyinString(metaUserDictItem.getDictName()));

            sysDict.setDictParentId(-1L);

            sysDict.setDictPids("[-1],");

            sysDict.setStatusFlag(StatusEnum.ENABLE.getCode());

            sysDict.setDictSort(metaUserDictItem.getDictSort());

            sysDictList.add(sysDict);
        }
        return sysDictList;
    }

    private static SysDictType createSysDictType(MetaUserDict metaUserDict) {
        SysDictType sysDictType = new SysDictType();

        sysDictType.setDictTypeId(metaUserDict.getMetaUserDictId());

        sysDictType.setDictTypeClass(DictTypeClassEnum.BUSINESS_TYPE.getCode());

        sysDictType.setDictTypeBusCode("");

        sysDictType.setDictTypeCode(metaUserDict.getDictTypeCode());

        sysDictType.setDictTypeName(metaUserDict.getDictTypeName());

        PinYinApi pinYinApi = SpringUtil.getBean(PinYinApi.class);
        sysDictType.setDictTypeNamePinyin(pinYinApi.parsePinyinString(metaUserDict.getDictTypeName()));

        sysDictType.setDictTypeDesc(metaUserDict.getDictTypeName());

        sysDictType.setStatusFlag(StatusEnum.ENABLE.getCode());

        sysDictType.setDictTypeSort(metaUserDict.getDictTypeSort());

        return sysDictType;
    }

}
