package com.ningdatech.pmapi.expert.assembler;

import cn.hutool.core.collection.CollectionUtil;
import com.ningdatech.pmapi.common.helper.RegionCacheHelper;
import com.ningdatech.pmapi.expert.constant.ExpertAccountStatusEnum;
import com.ningdatech.pmapi.expert.constant.QueryExpertAccountStatusEnum;
import com.ningdatech.pmapi.expert.model.DictionaryFieldInfo;
import com.ningdatech.pmapi.expert.model.TagFieldInfo;
import com.ningdatech.pmapi.expert.model.dto.ExpertAdminExpertManageListDTO;
import com.ningdatech.pmapi.expert.model.vo.ExpertAdminExpertManageListVO;
import com.ningdatech.pmapi.meta.helper.DictionaryCache;
import com.ningdatech.pmapi.meta.helper.impl.TagsCacheImpl;
import com.ningdatech.pmapi.meta.model.ExpertRegionInfo;
import com.ningdatech.pmapi.meta.model.dto.DictionaryDTO;
import com.ningdatech.pmapi.meta.model.dto.TagDTO;
import com.ningdatech.pmapi.sys.model.dto.RegionDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/8/5 下午4:32
 */
@Component
@RequiredArgsConstructor
public class ExpertAdminExpertManageAssembler {

    private final DictionaryCache dictionaryCache;
    private final TagsCacheImpl tagCache;
    private final RegionCacheHelper regionCache;

    public List<ExpertAdminExpertManageListVO> toExpertAdminExpertManageListVOList(
            List<ExpertAdminExpertManageListDTO> expertManageListDTOList) {
        if (CollectionUtil.isEmpty(expertManageListDTOList)) {
            return new ArrayList<>();
        }
        return expertManageListDTOList.stream().map(r -> {
            ExpertAdminExpertManageListVO expertAdminExpertManageListVO = new ExpertAdminExpertManageListVO();
            expertAdminExpertManageListVO.setUserId(r.getUserId());
            expertAdminExpertManageListVO.setExpertName(r.getExpertName());
            expertAdminExpertManageListVO.setPhoneNo(r.getPhoneNo());
            expertAdminExpertManageListVO.setIsDingUser(r.getIsDingUser());
            expertAdminExpertManageListVO.setCompany(r.getCompany());
            expertAdminExpertManageListVO.setLegalEntityCode(r.getLegalEntityCode());

            if (StringUtils.isNotBlank(r.getExpertAccountStatus())) {
                ExpertAccountStatusEnum statusEnum = ExpertAccountStatusEnum.of(r.getExpertAccountStatus());
                switch (statusEnum) {
                    case FREEZE:
                        expertAdminExpertManageListVO.setExpertAccountStatus(QueryExpertAccountStatusEnum.FREEZING.getKey());
                        break;
                    case AVAILABLE:
                        expertAdminExpertManageListVO.setExpertAccountStatus(QueryExpertAccountStatusEnum.NORMAL.getKey());
                        break;
                    default:
                        break;
                }
            }
            ExpertRegionInfo expertRegionInfo = r.getExpertRegionInfo();
            if (Objects.nonNull(expertRegionInfo)) {
                Integer regionLevel = expertRegionInfo.getRegionLevel();
                String regionCode = expertRegionInfo.getRegionCode();
                RegionDTO regionDTO = regionCache.getByCodeAndLevel(regionCode, regionLevel);
                if (Objects.nonNull(regionDTO)) {
                    expertRegionInfo.setRegionCode(regionDTO.getRegionCode());
                    expertRegionInfo.setRegionLevel(regionDTO.getRegionLevel());
                    expertRegionInfo.setRegionName(regionDTO.getRegionName());
                    expertAdminExpertManageListVO.setExpertRegionInfo(expertRegionInfo);
                }
            }
            List<DictionaryFieldInfo> expertType = r.getExpertType();
            if (CollectionUtils.isNotEmpty(expertType)) {
                expertType = assembleDictionaryName(expertType);
                expertAdminExpertManageListVO.setExpertType(expertType);
            }
            List<DictionaryFieldInfo> companyAttribute = r.getCompanyAttribute();
            if (CollectionUtils.isNotEmpty(companyAttribute)) {
                companyAttribute = assembleDictionaryName(companyAttribute);
                expertAdminExpertManageListVO.setCompanyAttribute(companyAttribute);
            }
            List<DictionaryFieldInfo> titleLevel = r.getTitleLevel();
            if (CollectionUtils.isNotEmpty(titleLevel)) {
                titleLevel = assembleDictionaryName(titleLevel);
                expertAdminExpertManageListVO.setTitleLevel(titleLevel);
            }

            List<TagFieldInfo> expertSource = r.getExpertSource();
            if (CollectionUtils.isNotEmpty(expertSource)) {
                expertSource = assembleTagName(expertSource);
                expertAdminExpertManageListVO.setExpertSource(expertSource);
            }
            expertAdminExpertManageListVO.setCreateTime(r.getCreateTime());
            return expertAdminExpertManageListVO;
        }).collect(Collectors.toList());

    }

    public List<DictionaryFieldInfo> assembleDictionaryName(List<DictionaryFieldInfo> collect) {
        if (CollectionUtil.isEmpty(collect)){
            return new ArrayList<>();
        }
        return collect.stream().peek(r -> {
            DictionaryDTO dictionaryDTO = dictionaryCache.getByCode(r.getDictionaryCode());
            if (Objects.nonNull(dictionaryDTO)) {
                r.setDictionaryName(dictionaryDTO.getName());
            }
        }).collect(Collectors.toList());
    }


    public List<TagFieldInfo> assembleTagName(List<TagFieldInfo> collect) {
        if (CollectionUtil.isEmpty(collect)){
            return new ArrayList<>();
        }
        return collect.stream().peek(r -> {
            TagDTO tagDTO = tagCache.getByTagCode(r.getTagCode());
            if (Objects.nonNull(tagDTO)) {
                r.setTagName(tagDTO.getTagName());
            }
        }).collect(Collectors.toList());
    }

}
