package com.zhongshu.card.server.core.init;

import com.github.microservice.auth.security.type.AuthType;
import com.zhongshu.card.client.type.DictionaryType;
import com.zhongshu.card.server.core.dao.dictionary.DictionaryDao;
import com.zhongshu.card.server.core.dao.dictionary.DictionaryItemDao;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dataConfig.DictionaryInitConfig;
import com.zhongshu.card.server.core.domain.dictionary.Dictionary;
import com.zhongshu.card.server.core.domain.dictionary.DictionaryItem;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.service.dictionary.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class DictionaryInit implements ApplicationRunner {

    @Autowired
    DictionaryDao dictionaryDao;

    @Autowired
    DictionaryItemDao dictionaryItemDao;

    @Autowired
    DictionaryInitConfig dictionaryInitConfig;

    @Autowired
    OrganizationDao organizationDao;


    public void init(){
        List<DictionaryInitConfig.Dictionary> dictionaryList = dictionaryInitConfig.getDictionaryList();

        if (dictionaryList == null || dictionaryList.isEmpty()) {
            log.warn("字典初始化数据为空");
            return;
        }

        List<Organization> project = organizationDao.findByAuthType(AuthType.Project);

        if (project == null || project.isEmpty()) {
            return;
        }

        for (Organization organization : project) {
            initDictionary(organization.getProjectOid(), dictionaryList);
        }
        log.info("字典数据初始化完成！");
    }

    public void initDictionary(String projectId, List<DictionaryInitConfig.Dictionary> dictionaryList) {
        for(DictionaryInitConfig.Dictionary dict : dictionaryList){
            Dictionary dictionary = syncDictionary(projectId, dict);
            if (dict.getItem() != null) {
                for (DictionaryInitConfig.Dictionary item : dict.getItem()){
                    syncDictionaryItem(item, null, dictionary);
                }
            }
        }
    }

    private Dictionary syncDictionary(String projectId, DictionaryInitConfig.Dictionary dict) {
        Dictionary dictionary = dictionaryDao.findTopByProjectIdAndKey(projectId, dict.getKey());
        if (dictionary == null) {
            dictionary = new Dictionary();
            BeanUtils.copyProperties(dict, dictionary);
            dictionary.setProjectId(projectId);
            dictionary.setType(DictionaryType.Standard);
            dictionary.setDisabled(false);
            dictionaryDao.save(dictionary);
        }
        return dictionary;
    }

    private void syncDictionaryItem(DictionaryInitConfig.Dictionary dict, DictionaryItem parent, Dictionary dictionary){
        DictionaryItem item = dictionaryItemDao.findTopByDictionary_IdAndKey(dictionary.getId(), dict.getKey());

        if (item == null) {
            item = new DictionaryItem();
            BeanUtils.copyProperties(dict, item);
            if (parent != null) {
                item.setParentId(parent.getId());
            }
            item.setDictionary(dictionary);
            item.setType(DictionaryType.Standard);
            String id = new ObjectId().toHexString();
            item.setId(id);
            item.setIdString(id);
            item.setCreateTime(System.currentTimeMillis());
            item.setDisable(false);
            dictionaryItemDao.save(item);
        }

        if (dict.getItem() !=null && !dict.getItem().isEmpty()){
            for (DictionaryInitConfig.Dictionary children: dict.getItem()){
                syncDictionaryItem(children, item, dictionary);
            }
        }
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        init();
    }
}
