package com.gic.haoban.manage.web.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.base.api.common.ServiceResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.BatchAddMaterialDTO;
import com.gic.haoban.manage.api.dto.MaterialCategoryDTO;
import com.gic.haoban.manage.api.dto.MaterialDTO;
import com.gic.haoban.manage.api.service.MaterialApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.utils.EmojiFilterUtil;

@RestController
public class MaterialController extends WebBaseController {

    @Autowired
    private MaterialApiService materialApiService;

    /**
     * 素材分组新增
     *
     * @return
     */
    @RequestMapping("/mateial-add-category")
    public HaobanResponse materialAddCategory(MaterialCategoryDTO materialCategoryDTO) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = login.getWxEnterpriseId();
        materialCategoryDTO.setWxEnterpriseId(wxEnterpriseId);
        String categoryName = materialCategoryDTO.getCategoryName();
        String categoryParentId = materialCategoryDTO.getCategoryParentId();
        if (StringUtils.isAnyBlank(categoryParentId, categoryName)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        boolean flag = materialApiService.hasCategoryNameExsit(categoryName, categoryParentId, null);
        if (flag) {
            return resultResponse(HaoBanErrCode.ERR_10015);
        }
        materialApiService.insertCategory(materialCategoryDTO);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 素材分组查询
     *
     * @return
     */
    @RequestMapping("/category-list")
    public HaobanResponse categoryList(String wxEnterpriseId) {
        List<MaterialCategoryDTO> list = materialApiService.listCategory(wxEnterpriseId);
        return resultResponse(HaoBanErrCode.ERR_1, list);
    }

    /**
     * 素材分组修改
     *
     * @return
     */
    @RequestMapping("/category-edit")
    public HaobanResponse categoryEdit(MaterialCategoryDTO materialCategoryDTO) {
        String categoryName = materialCategoryDTO.getCategoryName();
        String categoryId = materialCategoryDTO.getCategoryId();
        if (StringUtils.isAnyBlank(categoryName, categoryId)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        MaterialCategoryDTO dto = materialApiService.selectMaterialCategoryById(categoryId);
        if (dto == null) {
            return resultResponse(HaoBanErrCode.ERR_10016);
        }
        String categoryParentId = dto.getCategoryParentId();
        boolean flag = materialApiService.hasCategoryNameExsit(categoryName, categoryParentId, materialCategoryDTO.getCategoryId());
        if (flag) {
            return resultResponse(HaoBanErrCode.ERR_10015);
        }
        materialApiService.editCategory(materialCategoryDTO);
        return resultResponse(HaoBanErrCode.ERR_1);
    }


    /**
     * 素材分组删除
     *
     * @return
     */
    @RequestMapping("/category-del")
    public HaobanResponse categoryDel(String categoryId) {
        MaterialCategoryDTO dto = materialApiService.selectMaterialCategoryById(categoryId);
        if (dto == null) {
            return resultResponse(HaoBanErrCode.ERR_10016);
        }
        List<MaterialCategoryDTO> categoryList = materialApiService.listByParentCategory(categoryId);
        if (categoryList != null && !categoryList.isEmpty()) {
            return resultResponse(HaoBanErrCode.ERR_10017);
        }

        List<MaterialDTO> list = materialApiService.listMaterialByCategoryId(categoryId);
        if (list != null && !list.isEmpty()) {
            return resultResponse(HaoBanErrCode.ERR_10017);
        }
        dto.setStatusFlag(0);
        materialApiService.editCategory(dto);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 素材新增
     *
     * @return
     */
    @RequestMapping("/material-add")
    public HaobanResponse materialAdd(MaterialDTO materialDTO) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = login.getWxEnterpriseId();
        materialDTO.setWxEnterpriseId(wxEnterpriseId);

        materialDTO.setStaffId(login.getClerkId());
        materialDTO.setStaffName(login.getClerkName());
        String msg = this.checkEmoji(materialDTO.getMaterialContent()) ;
        if(null != msg) {
            return this.fail(msg) ;
        }
        String materialTitle = materialDTO.getMaterialTitle();
        String categoryId = materialDTO.getCategoryId();
        Integer categoryType = materialDTO.getMaterialType();
        if (StringUtils.isAnyBlank(categoryId, materialTitle)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        if (categoryType == null) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        materialApiService.insertMaterial(materialDTO);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 批量新增素材
     *
     * @param dto dto
     * @return res
     */
	@RequestMapping("/material-batch-add")
    public HaobanResponse batchAddMaterial(@RequestBody BatchAddMaterialDTO dto) {
        if (Objects.isNull(dto) || Objects.isNull(dto.getMaterial())) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        BatchAddMaterialDTO.Material material = dto.getMaterial();
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        material.setWxEnterpriseId(login.getWxEnterpriseId());
        material.setStaffId(login.getClerkId());
        material.setStaffName(null);
        if (StringUtils.isAnyBlank(material.getCategoryId(), material.getMaterialTitle())
                || Objects.isNull(material.getMaterialType())) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        materialApiService.batchInsertMaterial(dto);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 素材详情
     *
     * @return
     */
    @RequestMapping("/material-detail")
    public HaobanResponse materialDetail(String materialId) {
        MaterialDTO dto = materialApiService.selectMaterialById(materialId);
        return resultResponse(HaoBanErrCode.ERR_1, dto);
    }

    /**
     * 素材修改
     *
     * @return
     */
    @RequestMapping("/material-edit")
    public HaobanResponse materialEdit(MaterialDTO materialDTO) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        materialDTO.setStaffId(login.getClerkId());
        materialDTO.setStaffName(login.getClerkName());
        String materialId = materialDTO.getMaterialId();
        String msg = this.checkEmoji(materialDTO.getMaterialContent()) ;
        if(null != msg) {
            return this.fail(msg) ;
        }
        MaterialDTO dto = materialApiService.selectMaterialById(materialId);
        if (dto == null) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        materialApiService.editMaterial(materialDTO);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 素材删除
     *
     * @return
     */
    @RequestMapping("/material-del")
    public HaobanResponse materialEdit(String materialIds) {
        if (StringUtils.isBlank(materialIds)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        String[] stringArr = materialIds.split(",");
        List<String> list = Arrays.asList(stringArr);

        materialApiService.delMaterial(list);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 素材查询
     *
     * @return
     */
    @RequestMapping("/material-list")
    public HaobanResponse materialList(String wxEnterpriseId, String keyword, String categoryId, Integer materialType, BasePageInfo pageInfo) {
        Page<MaterialDTO> page = materialApiService.listMaterial(wxEnterpriseId, keyword, categoryId, materialType, pageInfo);
        return resultResponse(HaoBanErrCode.ERR_1, page);
    }

    /**
     * 素材批量转移
     *
     * @return
     */
    @RequestMapping("/material-batch")
    public HaobanResponse materialBatch(String materialIds, String categoryId) {
        String[] stringArr = materialIds.split(",");
        for (String materialId : stringArr) {
            MaterialDTO dto = materialApiService.selectMaterialById(materialId);
            if (dto == null) {
                continue;
            }
            dto.setCategoryId(categoryId);
            materialApiService.editMaterial(dto);
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 分析素材
     *
     * @return
     */
    @RequestMapping("/material-share")
    public HaobanResponse materialShare(String materialIds, String toWxEnterpriseIds) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        String[] wxEids = toWxEnterpriseIds.split(",");
        String[] maIds = materialIds.split(",");
        for (String maId : maIds) {
            for (String wxEid : wxEids) {
                ServiceResponse<Void> response = materialApiService.shareMaterial(login.getWxEnterpriseId(), wxEid, maId);
                if (response.getCode() == 1 || response.getCode() == 4) {
                    continue;
                }
                HaoBanErrCode.ERR_DEFINE.setMsg(response.getMessage());
                return resultResponse(HaoBanErrCode.ERR_DEFINE);
            }
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }
    
    public static String checkEmoji(String msg) {
        if(StringUtils.isBlank(msg)) {
            return null ;
        }
        String s = EmojiFilterUtil.filterEmoji(msg) ;
        if(!msg.equals(s)) {
            return "保存失败，不支持特殊表情字符" ;
        }
        return null ;
    }
    
    public static void main(String[] args) {
        System.out.println(EmojiFilterUtil.filterEmoji("！@#~！@#￥%……&*&）（）——+——+{}“”：？》") );;
    }
}
