package com.github.wxiaoqi.security.admin.feign.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.wxiaoqi.security.admin.feign.UserFeign;
import com.github.wxiaoqi.security.admin.feign.dto.AppUserDTO;
import com.github.wxiaoqi.security.admin.feign.dto.UserDTO;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.vo.DataInter;
import com.github.wxiaoqi.security.common.vo.DataInterBean;
import org.slf4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public interface UserRestInterface  {
    public UserFeign getUserFeign();

    default HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    default AppUserDTO getAppUser(){
        return  getUserFeign().userDetailByToken(BaseContextHandler.getToken()).getData();
    }

    default void checkAppUser(){
        if(null == getAppUser()) {
            throw new BaseException(ResultCode.DB_OPERATION_FAIL_CODE);
        }
    }

    default UserDTO getAdminUserInfo() {
        if(getRequest().getHeader("Authorization") !=null) {
            return getUserFeign().userinfoByToken(getRequest().getHeader("Authorization")).getData();
        }else {
            return null;
        }
    }

    default UserDTO getAdminUserInfoV2(boolean flag) {
        if(getRequest().getHeader("Authorization") !=null) {
            return getUserFeign().userinfoByTokenV2(getRequest().getHeader("Authorization"),flag).getData();
        }else {
            return null;
        }
    }

    default UserDTO getAdminUserInfoV2() {
        return getAdminUserInfoV2(Boolean.TRUE);
    }

    default void checkAdminUser(){
        if(null == getAdminUserInfo()) {
            throw new BaseException(ResultCode.NOTEXIST_CODE);
        }
    }

    default  void  setPowerData(DataInter dataInter,boolean flag){
        UserDTO userDTO = getAdminUserInfoV2(flag);
        if (userDTO == null) {
            return;
        }
        dataInter.setBizType(userDTO.getBizType());
        Integer dataAll=userDTO.getDataAll()== null ? 2 : userDTO.getDataAll();
        System.out.println("---dataAll==="+dataAll);
        if (dataAll == 2){
            List<Integer> dataCompany2List = userDTO.dataCompany2List();
            if (dataCompany2List != null && dataCompany2List.size() > 0){
                dataInter.setDataCompanyIds(dataCompany2List);
            }else {
                List<Integer> dataCorporation2List = userDTO.dataCorporation2List();
                if (dataCorporation2List != null && dataCorporation2List.size() > 0){
                    if (flag){
                        dataInter.setDataCorporationIds(dataCorporation2List);
                    }else {
                        dataInter.setDataCompanyIds(dataCompany2List);
                    }

                }else {
                    List<Integer> ids=new ArrayList<>();
                    ids.add(0);
                    dataInter.setDataCompanyIds(ids);
                }
            }
        }
    }

    default void setPowerData(DataInter dataInter){
        setPowerData(dataInter, Boolean.TRUE);
    }

    default DataInterBean getDataInter() {
        DataInterBean dataInterBean = new DataInterBean();
        setPowerData(dataInterBean);
        return dataInterBean;
    }

    default List<Integer> getCompanyIds(){
        UserDTO userDTO = getAdminUserInfoV2(Boolean.FALSE);
        if (userDTO != null){
            return  userDTO.getCompanyIds();
        }
        return  null;
    }

    default List<Integer> getBusinessUserCompanyIds(){
        UserDTO userDTO = getBusinessUserByAppUser();
        if (getBusinessUserByAppUser() != null){
            return userDTO.getCompanyIds();
        }
        return  null;
    }

    default UserDTO getBusinessUserByAppUser(){
        String currentUserName = BaseContextHandler.getUsername();
        if (StrUtil.isNotBlank(currentUserName)){
            ObjectRestResponse<UserDTO> restResponse = getUserFeign().businessUserinfoByMobilePhone(currentUserName);
            return restResponse.getData();
        }
        return  null;
    }

    default  void  setPowerDataCorporation(DataInter dataInter){
        UserDTO userDTO = getAdminUserInfoV2(true);
        if (userDTO == null) {
            return;
        }
        dataInter.setBizType(userDTO.getBizType());
        Integer dataAll=userDTO.getDataAll()== null ? 2 : userDTO.getDataAll();
        if (dataAll == 2){
            List<Integer> dataCorporation2List = userDTO.dataCorporation2List() == null ? new ArrayList<>() : userDTO.dataCorporation2List();
            if (dataCorporation2List.size() == 0) {
                dataCorporation2List.add(0);
            }
            dataInter.setDataCorporationIds(dataCorporation2List);
        }
    }

}
