package com.ningdatech.pmapi.expert.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.pmapi.common.helper.RegionLimitHelper;
import com.ningdatech.pmapi.expert.constant.ExpertAccountStatusEnum;
import com.ningdatech.pmapi.expert.constant.QueryExpertAccountStatusEnum;
import com.ningdatech.pmapi.expert.helper.PermissionCheckHelper;
import com.ningdatech.pmapi.expert.model.ExpertAdminExpertManageQuery;
import com.ningdatech.pmapi.expert.model.cmd.ExpertAdminExpertManageQueryCmd;
import com.ningdatech.pmapi.expert.model.query.ExpertDictionaryQuery;
import com.ningdatech.pmapi.expert.model.query.ExpertTagQuery;
import com.ningdatech.pmapi.expert.model.vo.ExpertAdminExpertManageListVO;
import com.ningdatech.pmapi.meta.constant.DictExpertInfoTypeEnum;
import com.ningdatech.pmapi.meta.constant.ExpertTagEnum;
import com.ningdatech.pmapi.meta.model.bo.RegionContainsBO;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * @author liuxinxin
 * @date 2023/3/1 下午2:12
 */

@Component
@RequiredArgsConstructor
public class ExpertAdminManage {

    private final PermissionCheckHelper permissionCheckHelper;
    private final RegionLimitHelper regionLimitHelper;

    /**
     * 专家管理员使用 专家库列表查询
     *
     * @param query
     * @return
     */
    public PageVo<ExpertAdminExpertManageListVO> getExpertLibraryList(ExpertAdminExpertManageQuery query) {
        Long userId = LoginUserUtil.getUserId();
        ExpertAdminExpertManageQueryCmd expertAdminExpertManageQueryCmd = buildExpertAdminExpertManageQueryCmd(query, userId);
//        CommonPage<ExpertAdminExpertManageListDTO> pageResult =
//                expertAdminManageService.getExpertLibraryList(expertAdminExpertManageQueryCmd);
//
//        PageVo<ExpertAdminExpertManageListVO> pageVo = new PageVo<>();
//        pageVo.setTotal(pageResult.getItemsTotal());
//        pageVo.setRecords(expertAdminExpertManageAssembler.toExpertAdminExpertManageListVOList(pageResult.getItems()));
//        return pageVo;
        return null;
    }


    private ExpertAdminExpertManageQueryCmd buildExpertAdminExpertManageQueryCmd(ExpertAdminExpertManageQuery query, Long userId) {
        ExpertAdminExpertManageQueryCmd expertAdminExpertManageQueryCmd = new ExpertAdminExpertManageQueryCmd();
        expertAdminExpertManageQueryCmd.setPageNumber(query.getPageNumber());
        expertAdminExpertManageQueryCmd.setPageSize(query.getPageSize());
        if (StringUtils.isNotBlank(query.getExpertName())) {
            expertAdminExpertManageQueryCmd.setExpertName(query.getExpertName());
        }
        if (StringUtils.isNotBlank(query.getCompany())) {
            expertAdminExpertManageQueryCmd.setCompany(query.getCompany());
        }
        List<ExpertDictionaryQuery> expertDictionaryQueryList = new ArrayList<>();
        if (StringUtils.isNotBlank(query.getTitleLevelDictionaryCode())) {
            ExpertDictionaryQuery expertDictionaryQuery = new ExpertDictionaryQuery();
            expertDictionaryQuery.setExpertInfoField(DictExpertInfoTypeEnum.TITLE_LEVEL.getKey());
            expertDictionaryQuery.setDictionaryCodeList(CollectionUtil.toList(query.getTitleLevelDictionaryCode()));
            expertDictionaryQueryList.add(expertDictionaryQuery);
        }
        if (StringUtils.isNotBlank(query.getCompanyAttributeDictionaryCode())) {
            ExpertDictionaryQuery expertDictionaryQuery = new ExpertDictionaryQuery();
            expertDictionaryQuery.setExpertInfoField(DictExpertInfoTypeEnum.COMPANY_ATTRIBUTE.getKey());
            expertDictionaryQuery.setDictionaryCodeList(CollectionUtil.toList(query.getCompanyAttributeDictionaryCode()));
            expertDictionaryQueryList.add(expertDictionaryQuery);
        }
        if (StringUtils.isNotBlank(query.getExpertTypeDictionaryCode())) {
            ExpertDictionaryQuery expertDictionaryQuery = new ExpertDictionaryQuery();
            expertDictionaryQuery.setExpertInfoField(DictExpertInfoTypeEnum.EXPERT_TYPE.getKey());
            expertDictionaryQuery.setDictionaryCodeList(CollectionUtil.toList(query.getExpertTypeDictionaryCode()));
            expertDictionaryQueryList.add(expertDictionaryQuery);
        }
        List<ExpertTagQuery> expertTagQueryList = new ArrayList<>();
        if (StringUtils.isNotBlank(query.getExpertSourceTagCode())) {
            ExpertTagQuery expertTagQuery = new ExpertTagQuery();
            expertTagQuery.setExpertInfoField(ExpertTagEnum.EXPERT_SOURCE.getKey());
            expertTagQuery.setTagCodeList(CollectionUtil.toList(query.getExpertSourceTagCode()));
            expertTagQueryList.add(expertTagQuery);
        }
        if (StringUtils.isNotBlank(query.getGoodAtTagCode())) {
            ExpertTagQuery expertTagQuery = new ExpertTagQuery();
            expertTagQuery.setExpertInfoField(ExpertTagEnum.GOOD_AT.getKey());
            expertTagQuery.setTagCodeList(CollectionUtil.toList(query.getGoodAtTagCode()));
            expertTagQueryList.add(expertTagQuery);
        }
        if (StringUtils.isNotBlank(query.getExpertAccountStatus())) {
            QueryExpertAccountStatusEnum queryExpertAccountStatusEnum = QueryExpertAccountStatusEnum.of(query.getExpertAccountStatus());
            switch (queryExpertAccountStatusEnum) {
                case NORMAL:
                    expertAdminExpertManageQueryCmd
                            .setExpertAccountStatusList(CollectionUtil.toList(ExpertAccountStatusEnum.AVAILABLE));
                    break;
                case FREEZING:
                    expertAdminExpertManageQueryCmd
                            .setExpertAccountStatusList(CollectionUtil.toList(ExpertAccountStatusEnum.FREEZE));
                    break;
                default:
                    break;
            }
        } else {
            expertAdminExpertManageQueryCmd
                    .setExpertAccountStatusList(CollectionUtil.toList(ExpertAccountStatusEnum.AVAILABLE, ExpertAccountStatusEnum.FREEZE));
        }

        List<RegionContainsBO> expertAdminContainsRegionList = new ArrayList<>();

        // 如果为超级管理员，可以查看所有的专家，包括没有层级的专家
        if (!permissionCheckHelper.isSuperAdmin()) {
            expertAdminContainsRegionList = regionLimitHelper.getExpertAdminContainsRegion(userId);
        }
        List<RegionContainsBO> containsRegion = new ArrayList<>();
        if (Objects.nonNull(query.getExpertRegionInfo())) {
            RegionContainsBO containsRegionBo = regionLimitHelper.getContainsRegionBo(query.getExpertRegionInfo().getRegionLevel()
                    , query.getExpertRegionInfo().getRegionCode());
            containsRegion = regionLimitHelper.queryContainsRegionAssembler(containsRegionBo, expertAdminContainsRegionList);
        } else {
            containsRegion = expertAdminContainsRegionList;
        }
        expertAdminExpertManageQueryCmd.setExpertDictionaryQueryList(expertDictionaryQueryList);
        expertAdminExpertManageQueryCmd.setExpertTagQueryList(expertTagQueryList);
        expertAdminExpertManageQueryCmd.setIsDingUser(query.getIsDingUser());
        expertAdminExpertManageQueryCmd.setRegionContainsList(containsRegion);
        expertAdminExpertManageQueryCmd.setPageSize(query.getPageSize());
        expertAdminExpertManageQueryCmd.setPageNumber(query.getPageNumber());
        return expertAdminExpertManageQueryCmd;
    }

}
