package com.jwipc.sys.controller.pc;

import com.google.common.collect.Lists;
import com.jwipc.message.dto.MessageAddresseeDTO;
import com.jwipc.message.logic.MessageAddresseeLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.supplier.clent.dto.SupplierCountDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.constant.DealtTargetCategroy;
import com.jwipc.sys.dto.HomePageAttrDTO;
import com.jwipc.sys.dto.UserDWTDTO;
import com.jwipc.sys.logic.DealtInfoLogic;
import com.jwipc.sys.logic.HomeLogic;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping("")
@RequiredArgsConstructor
public class HomePageController {
    private final HomeLogic homeLogic;
    private final SupplierClient supplierClient;
    private final DealtInfoLogic dealtInfoLogic;
    private final MessageAddresseeLogic messageAddresseeLogic;

    @RequestMapping({"/", "/home"})
    public String index(Model model) {
        Locale locale = LocaleContextHolder.getLocale();
        // 判断是否已经登录
        final CurrentUserDTO currentUser = LoginUtil.getCurrentUser();
        if (Objects.isNull(currentUser)) {
            return "redirect:" + "/logout";
        }
        long userId = currentUser.getUserId();
        //结束
        final Integer language = "en_US".equals(locale.toString()) ? 2 : 1;
        model.addAttribute("jt_srm_language", language);
        final List<Long> roleIds = currentUser.getRoleIds();
        List<String> sysConfigKeys = Lists.newArrayList();
        sysConfigKeys.add("homeLogo");
        sysConfigKeys.add("webUrlIco");
        sysConfigKeys.add("webUrlTitle");
        sysConfigKeys.add("processFlowHome");
        sysConfigKeys.add("pwd_reg");
        final HomePageAttrDTO homePageAttrDTO = homeLogic.renderHanlder(roleIds, sysConfigKeys);
        final Map<String, String> configs = homePageAttrDTO.getConfigs();
        for (String s : configs.keySet()) {
            model.addAttribute(s, configs.get(s));
        }
        model.addAttribute("displayMenuData", homePageAttrDTO.getDisplayMenuData());
        model.addAttribute("moreMenuData", homePageAttrDTO.getMoreMenuData());
        model.addAttribute("list", homePageAttrDTO.getNs());
        model.addAttribute("userInfo", currentUser);
        model.addAttribute("firstLogin", currentUser.getIsFirstLogin());
        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        Base64.Encoder encode = Base64.getEncoder();
        model.addAttribute("tokenUserId", encode.encode(currentUser.getUserId().toString().getBytes()));

        // 检查当前登录人是否分配了待办指标 todo 代办指标功能
        final boolean is = homeLogic.checkUserAllotDwt(userId, DealtTargetCategroy.DEALTWITH);
        //增加登录查询未读信息查询
        List<MessageAddresseeDTO> received = messageAddresseeLogic.getReceived(LoginUtil.getUserId());
        boolean hasUnread = false;
        if(CollectionUtils.isNotEmpty(received)){
             hasUnread = received.stream()
                    .anyMatch(dto -> dto.getSupplierIsRead() == 0);
        }
        model.addAttribute("hasUnread", hasUnread);
        model.addAttribute("donePage", is);
        return MODULE_ROOT + "/home/index";
    }

    @GetMapping(MODULE_ROOT + "/home/workbench/message")
    public String workbenchMessage(Model model) {
        model.addAttribute("isSup", LoginUtil.isSupplier());

        List<UserDWTDTO> userDWTDTOS = dealtInfoLogic.getUserDealtWith();
        boolean isHide = true;
        for (UserDWTDTO userDWTDTO : userDWTDTOS) {
            if ("MHGYSTJ".equals(userDWTDTO.getTargetTypeCode())) {
                isHide = false;
            }
        }

        model.addAttribute("isHide", isHide);
        //需要查询各个供应商的统计数量
        final SupplierCountDTO supplierCountDTO = supplierClient.countSupplier();
        model.addAttribute(supplierCountDTO);
        return MODULE_ROOT + "/home/message";
    }

    @GetMapping(MODULE_ROOT + "/home/workbench/done")
    public String workbenchDone(Model model) {
        model.addAttribute("isSup", LoginUtil.isSupplier());
        return MODULE_ROOT + "/home/done";
    }

}
