package com.jpsoft.shinestar.modules.mobile.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.config.RedisConfig;
import com.jpsoft.shinestar.modules.base.dto.IconInfoDTO;
import com.jpsoft.shinestar.modules.base.dto.IconListDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author 墨鱼_mo
 * @date 2020-10-10 14:41
 */

@RestController
@RequestMapping("/mobile/icon")
@Slf4j
public class IconApiController {

    @Autowired
    private IconInfoService iconInfoService;

    @Autowired
    private IconTypeService iconTypeService;

    @Autowired
    private PersonRoleIconService personRoleIconService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private PersonPopedomIconService personPopedomIconService;


    @PostMapping("findIconByPersonRole")
    @ApiOperation(value = "按照用户角色查询图标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> findIconByPersonRole(String token, @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.findById(Long.valueOf(subject));

            if (personInfo == null) {
                throw new Exception("当前用户不存在!");
            }

            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());


            //默认图标功能
            List<String> ids = new ArrayList<>();
            if (StringUtils.isNotBlank(personPopedom.getShowIconId())) {
                ids = Arrays.asList(personPopedom.getShowIconId().split(","));
            }

            List<PersonRoleIcon> personRoleIconList = personRoleIconService.findByPersonRoleId(personPopedom.getPersonRoleId());
            List<PersonPopedomIcon> personPopedomIconList = personPopedomIconService.findByPersonPopedomId(personPopedom.getId());


            //    List<IconListDTO> list = new ArrayList<>();
            List<Map<String, Object>> listMap = new ArrayList<>();
            Map<String, Object> map = new LinkedHashMap<>();
            List<IconInfo> tempList = new ArrayList<>();
            if (personRoleIconList.size() > 0) {
                for (PersonRoleIcon personRoleIcon : personRoleIconList) {
                    Map<String, Object> dataMap = new HashMap<>();
                    IconInfo iconInfo = iconInfoService.get(personRoleIcon.getIconId());
                    if (iconInfo != null && iconInfo.getUseEnable() && !personRoleIcon.getCommonStatus()) {
                        tempList.add(iconInfo);
                        IconType iconType = iconTypeService.get(iconInfo.getIconTypeId());
                        IconInfoDTO iconInfoDTO = new IconInfoDTO();
                        iconInfoDTO.setId(iconInfo.getId());
                        iconInfoDTO.setIconName(iconInfo.getIconName());
                        iconInfoDTO.setIconPic(iconInfo.getIconPic());
                        iconInfoDTO.setIconRoute(iconInfo.getIconRoute());
                        iconInfoDTO.setIconTypeId(iconInfo.getIconTypeId());
                        iconInfoDTO.setIconTypeName(iconType.getTypeName());
                        if (StringUtils.isNotBlank(iconInfo.getIconParam())) {
                            iconInfoDTO.setIconParam(JSONObject.parseObject(iconInfo.getIconParam()));
                        }
                        iconInfoDTO.setIconUrl(iconInfo.getIconUrl());
                        iconInfoDTO.setUseEnable(iconInfo.getUseEnable());
                        iconInfoDTO.setIconCommonPic(iconInfo.getIconCommonPic());
                        if (StringUtils.isNotBlank(iconInfo.getIconSubRoute())) {
                            iconInfoDTO.setIconSubRouteList(Arrays.asList(iconInfo.getIconSubRoute().split(",")));
                        }
                        List<IconInfoDTO> iconInfoDTOList = new ArrayList<>();
                        if (map.containsKey(iconType.getTypeName())) {
                            iconInfoDTOList = (List<IconInfoDTO>) map.get(iconType.getTypeName());

                        }
                        iconInfoDTOList.add(iconInfoDTO);
                        map.put(iconType.getTypeName(), iconInfoDTOList);

                    }
                }


            }

            if (personPopedomIconList.size() > 0) {
                for (PersonPopedomIcon personPopedomIcon : personPopedomIconList) {
                    Map<String, Object> dataMap = new HashMap<>();
                    IconInfo iconInfo = iconInfoService.get(personPopedomIcon.getIconId());
                    if (iconInfo != null && iconInfo.getUseEnable() && !tempList.contains(iconInfo)) {
                        IconType iconType = iconTypeService.get(iconInfo.getIconTypeId());
                        IconInfoDTO iconInfoDTO = new IconInfoDTO();
                        iconInfoDTO.setId(iconInfo.getId());
                        iconInfoDTO.setIconName(iconInfo.getIconName());
                        iconInfoDTO.setIconPic(iconInfo.getIconPic());
                        iconInfoDTO.setIconRoute(iconInfo.getIconRoute());
                        iconInfoDTO.setIconTypeId(iconInfo.getIconTypeId());
                        iconInfoDTO.setIconTypeName(iconType.getTypeName());
                        if (StringUtils.isNotBlank(iconInfo.getIconParam())) {
                            iconInfoDTO.setIconParam(JSONObject.parseObject(iconInfo.getIconParam()));
                        }
                        iconInfoDTO.setIconUrl(iconInfo.getIconUrl());
                        iconInfoDTO.setUseEnable(iconInfo.getUseEnable());
                        iconInfoDTO.setIconCommonPic(iconInfo.getIconCommonPic());
                        if (StringUtils.isNotBlank(iconInfo.getIconSubRoute())) {
                            iconInfoDTO.setIconSubRouteList(Arrays.asList(iconInfo.getIconSubRoute().split(",")));
                        }
                        List<IconInfoDTO> iconInfoDTOList = new ArrayList<>();
                        if (map.containsKey(iconType.getTypeName())) {
                            iconInfoDTOList = (List<IconInfoDTO>) map.get(iconType.getTypeName());

                        }
                        iconInfoDTOList.add(iconInfoDTO);
                        map.put(iconType.getTypeName(), iconInfoDTOList);

                    }
                }

                /*Set set = map.keySet();
                for (Object key : set) {
                    Map<String, Object> resutlMap = new HashMap<>();
                    resutlMap.put("name", key);
                    resutlMap.put("list", map.get(key));
                    listMap.add(resutlMap);
                }*/
            }

            Set set = map.keySet();
            for (Object key : set) {
                Map<String, Object> resutlMap = new HashMap<>();
                resutlMap.put("name", key);
                resutlMap.put("list", map.get(key));
                listMap.add(resutlMap);
            }


            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("allIcon", listMap);


            //查询此角色用户下的显示图标
            List<IconInfoDTO> iconInfoList = new ArrayList<>();
            if (StringUtils.isNotBlank(personPopedom.getShowIconId())) {
                List<String> idList = Arrays.asList(personPopedom.getShowIconId().split(","));

                for (String id : idList) {
                    //    PersonRoleIcon personRoleIcon = personRoleIconService.get(id);
                    //    if (personRoleIcon != null){
                    IconInfo iconInfo = iconInfoService.get(id);
                    //   PersonRoleIcon personRoleIcon = personRoleIconService.findByPersonRoleIdAndIconId(personRoleInfo.getId(),id);
                    //     if (iconInfo != null && iconInfo.getUseEnable() && personRoleIcon != null) {
                    IconInfoDTO iconInfoDTO = new IconInfoDTO();
                    if (iconInfo != null) {
                        BeanUtils.copyProperties(iconInfo, iconInfoDTO);
                        if (StringUtils.isNotBlank(iconInfo.getIconParam())) {
                            iconInfoDTO.setIconParam(JSONObject.parseObject(iconInfo.getIconParam()));
                        }

                        if (iconInfoList.size() < 7) {
                            iconInfoList.add(iconInfoDTO);
                        }
                        if (iconInfoList.size() >= 7) {
                            break;
                        }
                    }

                }


            }
            dataMap.put("iconInfoList", iconInfoList);


            messageResult.setData(dataMap);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }


    @PostMapping("findAllIconByPersonRole")
    @ApiOperation(value = "按照用户角色查询所有图标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> findAllIconByPersonRole(String token, @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.findById(Long.valueOf(subject));

            if (personInfo == null) {
                throw new Exception("当前用户不存在!");
            }

            List<Map<String, Object>> listMap = new ArrayList<>();
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());

            if (personPopedom != null) {


                //默认图标功能
                List<String> ids = new ArrayList<>();
                if (StringUtils.isNotBlank(personPopedom.getShowIconId())) {
                    ids = Arrays.asList(personPopedom.getShowIconId().split(","));
                }

                List<PersonRoleIcon> personRoleIconList = personRoleIconService.findByPersonRoleId(personPopedom.getPersonRoleId());
                List<PersonPopedomIcon> personPopedomIconList = personPopedomIconService.findByPersonPopedomId(personPopedom.getId());
                //    List<IconListDTO> list = new ArrayList<>();

                Map<String, Object> map = new LinkedHashMap<>();
                if (personRoleIconList.size() > 0) {
                    for (PersonRoleIcon personRoleIcon : personRoleIconList) {
                        Map<String, Object> dataMap = new HashMap<>();
                        IconInfo iconInfo = iconInfoService.get(personRoleIcon.getIconId());
                        if (iconInfo != null && iconInfo.getUseEnable()) {
                            IconType iconType = iconTypeService.get(iconInfo.getIconTypeId());
                            IconInfoDTO iconInfoDTO = new IconInfoDTO();
                            iconInfoDTO.setId(iconInfo.getId());
                            iconInfoDTO.setIconName(iconInfo.getIconName());
                            iconInfoDTO.setIconPic(iconInfo.getIconPic());
                            iconInfoDTO.setIconRoute(iconInfo.getIconRoute());
                            iconInfoDTO.setIconTypeId(iconInfo.getIconTypeId());
                            iconInfoDTO.setIconTypeName(iconType.getTypeName());
                            if (StringUtils.isNotBlank(iconInfo.getIconParam())) {
                                iconInfoDTO.setIconParam(JSONObject.parseObject(iconInfo.getIconParam()));
                            }
                            iconInfoDTO.setIconUrl(iconInfo.getIconUrl());
                            iconInfoDTO.setUseEnable(iconInfo.getUseEnable());
                            iconInfoDTO.setIconCommonPic(iconInfo.getIconCommonPic());
                            if (StringUtils.isNotBlank(iconInfo.getIconSubRoute())) {
                                iconInfoDTO.setIconSubRouteList(Arrays.asList(iconInfo.getIconSubRoute().split(",")));
                            }
                            List<IconInfoDTO> iconInfoDTOList = new ArrayList<>();
                            if (map.containsKey(iconType.getTypeName())) {
                                iconInfoDTOList = (List<IconInfoDTO>) map.get(iconType.getTypeName());

                            }
                            iconInfoDTOList.add(iconInfoDTO);
                            map.put(iconType.getTypeName(), iconInfoDTOList);

                        }
                    }

                   /* Set set = map.keySet();
                    for (Object key : set) {
                        Map<String, Object> resutlMap = new HashMap<>();
                        resutlMap.put("name", key);
                        resutlMap.put("list", map.get(key));
                        listMap.add(resutlMap);
                    }*/
                }

                if (personPopedomIconList.size() > 0) {
                    for (PersonPopedomIcon personPopedomIcon : personPopedomIconList) {
                        Map<String, Object> dataMap = new HashMap<>();
                        IconInfo iconInfo = iconInfoService.get(personPopedomIcon.getIconId());
                        if (iconInfo != null && iconInfo.getUseEnable()) {
                            IconType iconType = iconTypeService.get(iconInfo.getIconTypeId());
                            IconInfoDTO iconInfoDTO = new IconInfoDTO();
                            iconInfoDTO.setId(iconInfo.getId());
                            iconInfoDTO.setIconName(iconInfo.getIconName());
                            iconInfoDTO.setIconPic(iconInfo.getIconPic());
                            iconInfoDTO.setIconRoute(iconInfo.getIconRoute());
                            iconInfoDTO.setIconTypeId(iconInfo.getIconTypeId());
                            iconInfoDTO.setIconTypeName(iconType.getTypeName());
                            if (StringUtils.isNotBlank(iconInfo.getIconParam())) {
                                iconInfoDTO.setIconParam(JSONObject.parseObject(iconInfo.getIconParam()));
                            }
                            iconInfoDTO.setIconUrl(iconInfo.getIconUrl());
                            iconInfoDTO.setUseEnable(iconInfo.getUseEnable());
                            iconInfoDTO.setIconCommonPic(iconInfo.getIconCommonPic());
                            if (StringUtils.isNotBlank(iconInfo.getIconSubRoute())) {
                                iconInfoDTO.setIconSubRouteList(Arrays.asList(iconInfo.getIconSubRoute().split(",")));
                            }
                            List<IconInfoDTO> iconInfoDTOList = new ArrayList<>();
                            if (map.containsKey(iconType.getTypeName())) {
                                iconInfoDTOList = (List<IconInfoDTO>) map.get(iconType.getTypeName());

                            }
                            iconInfoDTOList.add(iconInfoDTO);
                            map.put(iconType.getTypeName(), iconInfoDTOList);

                        }
                    }

                /*Set set = map.keySet();
                for (Object key : set) {
                    Map<String, Object> resutlMap = new HashMap<>();
                    resutlMap.put("name", key);
                    resutlMap.put("list", map.get(key));
                    listMap.add(resutlMap);
                }*/
                }
                Set set = map.keySet();
                for (Object key : set) {
                    Map<String, Object> resutlMap = new HashMap<>();
                    resutlMap.put("name", key);
                    resutlMap.put("list", map.get(key));
                    listMap.add(resutlMap);
                }


            }
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("allIcon", listMap);


            messageResult.setData(dataMap);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("updatePersonIcon")
    @ApiOperation(value = "更新个人功能图标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "form"),

    })
    public MessageResult<List> updatePersonIcon(String token, @RequestAttribute String subject, String ids) {
        MessageResult<List> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.findById(Long.valueOf(subject));

            if (personInfo == null) {
                throw new Exception("当前用户不存在!");
            }

            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());

            personPopedom.setShowIconId(ids);
            personPopedomService.update(personPopedom);


            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }


    @PostMapping("findIconRouteByPerson")
    @ApiOperation(value = "按照用户查询所有图标路由")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> findIconRouteByPerson(String token, @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.findById(Long.valueOf(subject));

            if (personInfo == null) {
                throw new Exception("当前用户不存在!");
            }

            List<String> list = new ArrayList<>();
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());

            if (personPopedom != null) {
                List<PersonRoleIcon> personRoleIconList = personRoleIconService.findByPersonRoleId(personPopedom.getPersonRoleId());
                List<PersonPopedomIcon> personPopedomIconList = personPopedomIconService.findByPersonPopedomId(personPopedom.getId());

                if (personRoleIconList.size() > 0) {
                    for (PersonRoleIcon personRoleIcon : personRoleIconList) {
                        IconInfo iconInfo = iconInfoService.get(personRoleIcon.getIconId());
                        if (!list.contains(iconInfo.getIconRoute()) && StringUtils.isNotBlank(iconInfo.getIconRoute())) {
                            list.add(iconInfo.getIconRoute());

                            if (StringUtils.isNotBlank(iconInfo.getIconSubRoute())) {
                                List<String> iconSubList = Arrays.asList(iconInfo.getIconSubRoute().split(","));
                                list.addAll(iconSubList);
                            }
                        }


                    }
                }

                if (personPopedomIconList.size() > 0) {
                    for (PersonPopedomIcon personPopedomIcon : personPopedomIconList) {
                        IconInfo iconInfo = iconInfoService.get(personPopedomIcon.getIconId());
                        if (iconInfo != null) {
                            if (!list.contains(iconInfo.getIconRoute()) && StringUtils.isNotBlank(iconInfo.getIconRoute())) {
                                list.add(iconInfo.getIconRoute());

                                if (StringUtils.isNotBlank(iconInfo.getIconSubRoute())) {
                                    List<String> iconSubList = Arrays.asList(iconInfo.getIconSubRoute().split(","));
                                    list.addAll(iconSubList);
                                }
                            }
                        }
                    }
                }


            }
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("allIconRoute", list);


            messageResult.setData(dataMap);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }


}
