package com.cool.store.vo.shop;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.point.ShopPointDTO;
import com.cool.store.entity.HyOpenAreaInfoDO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.vo.LinePointBaseInfoVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Author suzhuhong
 * @Date 2025/1/8 14:59
 * @Version 1.0
 */
@Data
public class ShopPointBaseInfoVO {

    @ApiModelProperty("门店ID")
    private Long shopId;

    private String partnerName;

    @ApiModelProperty("门店名称")
    private String shopName;

    @ApiModelProperty("手机号")
    private String mobile;

    @ApiModelProperty("区域名称")
    private String areaName;

    @ApiModelProperty("区域路径")
    private String areaPath;

    @ApiModelProperty("用户标签")
    private List<String> userPortraitList;

    @ApiModelProperty("招商经理")
    private String investmentManagerUsername;

    @ApiModelProperty("已推门店")
    private Integer recommendShopNum;

    @ApiModelProperty("已选门店")
    private Integer selectedShopNum;

    public ShopPointBaseInfoVO(Long shopId, String shopName, String mobile) {
        this.shopId = shopId;
        this.shopName = shopName;
        this.mobile = mobile;
    }

    public static List<ShopPointBaseInfoVO> convertList(Map<Long, String> partnerNameMap,List<ShopPointDTO> lineList, Map<String, String> userNameMap, Map<Long, String> userPortraitMap, Map<Long, HyOpenAreaInfoDO> cityMap, Map<Long, Integer> recommendShopNumMap){
        List<ShopPointBaseInfoVO> resultList = new ArrayList<>();
        for (ShopPointDTO shopPointDTO : lineList) {
            ShopPointBaseInfoVO shopPointBaseInfoVO = new ShopPointBaseInfoVO(shopPointDTO.getShopId(), shopPointDTO.getShopName(), shopPointDTO.getMobile());
            String username = userNameMap.get(shopPointDTO.getInvestmentManager());
            shopPointBaseInfoVO.setInvestmentManagerUsername(username);
            shopPointBaseInfoVO.setPartnerName(partnerNameMap.get(shopPointDTO.getLineId()));
            HyOpenAreaInfoDO areaInfo = cityMap.get(shopPointDTO.getWantShopAreaId());
            if(Objects.nonNull(areaInfo)){
                shopPointBaseInfoVO.setAreaName(areaInfo.getAreaName());
                shopPointBaseInfoVO.setAreaPath(areaInfo.getAreaPath().replace("/", "").trim());
            }
            shopPointBaseInfoVO.setRecommendShopNum(recommendShopNumMap.get(shopPointDTO.getShopId()));
            shopPointBaseInfoVO.setSelectedShopNum((shopPointDTO.getPointId()!=null&&shopPointDTO.getPointId()> CommonConstants.ZERO)?CommonConstants.ONE:CommonConstants.ZERO);
            if(StringUtils.isNotBlank(shopPointDTO.getUserPortrait())){
                List<String> userPortraitList = new ArrayList<>();
                String[] parts = shopPointDTO.getUserPortrait().split(",");
                for (String part : parts) {
                    String trimmedPart = part.trim();
                    if (StringUtils.isNotBlank(trimmedPart)) {
                        String s = userPortraitMap.get(Long.valueOf(part));
                        if(StringUtils.isNotBlank(s)){
                            userPortraitList.add(s);
                        }
                    }
                }
                shopPointBaseInfoVO.setUserPortraitList(userPortraitList);
            }
            resultList.add(shopPointBaseInfoVO);
        }
        return resultList;
    }


    public static ShopPointBaseInfoVO convert(LineInfoDO lineInfo, String username, Map<Long, String> userPortraitMap, HyOpenAreaInfoDO cityArea, Map<Long, Integer> recommendShopNumMap, Map<Long, Integer> selectedShopNumMap){
        ShopPointBaseInfoVO linePointBaseInfo = new ShopPointBaseInfoVO(lineInfo.getId(), lineInfo.getUsername(), lineInfo.getMobile());
        linePointBaseInfo.setInvestmentManagerUsername(username);
        if(Objects.nonNull(cityArea)){
            linePointBaseInfo.setAreaName(cityArea.getAreaName());
            linePointBaseInfo.setAreaPath(cityArea.getAreaPath().replace("/", "").trim());
        }
        linePointBaseInfo.setRecommendShopNum(recommendShopNumMap.get(lineInfo.getId()));
        linePointBaseInfo.setSelectedShopNum(selectedShopNumMap.get(lineInfo.getId()));
        if(StringUtils.isNotBlank(lineInfo.getUserPortrait())){
            List<String> userPortraitList = new ArrayList<>();
            String[] parts = lineInfo.getUserPortrait().split(",");
            for (String part : parts) {
                String trimmedPart = part.trim();
                if (StringUtils.isNotBlank(trimmedPart)) {
                    String s = userPortraitMap.get(Long.valueOf(part));
                    if(StringUtils.isNotBlank(s)){
                        userPortraitList.add(s);
                    }
                }
            }
            linePointBaseInfo.setUserPortraitList(userPortraitList);
        }
        return linePointBaseInfo;
    }



}
