package cn.com.countrygarden.bi.main.controller.experience;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultCode;
import cn.com.countrygarden.bi.main.dto.experience.WcxPublishContentDto;
import cn.com.countrygarden.bi.main.dto.experience.WcxPublishFileDto;
import cn.com.countrygarden.bi.main.model.wcx.WcxUser;
import cn.com.countrygarden.bi.main.service.experience.TygGzhH5Service;
import cn.com.countrygarden.bi.main.util.EmptyUtils;
import cn.com.countrygarden.bi.main.util.OSSUtils;
import cn.com.countrygarden.bi.main.util.SensitiveWordFilterUtil;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/tyg/gzh/h5")
public class TygGzhH5Controller {
    @Autowired
    private TygGzhH5Service tygGzgH5Service;
    @Autowired
    private OSSUtils ossUtils;

    @PostMapping("/index")
    @ResponseBody
    public Result index(@RequestParam(value="phone",required = false) String phone,
                        @RequestParam(value = "size",defaultValue = "10")int size,
                        @RequestParam(value = "page",defaultValue = "1")int page){
        return tygGzgH5Service.index(phone,size,page);
    }
    /**
     * 发布内容
     * @param openid
     * @param name
     * @param avatarUrl
     * @param phone
     * @param publishType
     * @param publishContent
     * @param fileDtos
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/contentPublish",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result publish(@RequestParam(name = "openid",required = false) String openid,
                          @RequestParam(name = "name",required = false) String name,
                          @RequestParam(name = "avatarUrl",required = false) String avatarUrl,
                          @RequestParam(name = "phone",required = false) String phone,
                          @RequestParam(name = "publishType",required = true) String publishType,
                          @RequestParam(name = "identity",required = true) String identity,
                          @RequestParam(name = "publishContent",required = false) String publishContent,
                          @RequestParam(name = "fileDtos", required = false, defaultValue = "") List<String> fileDtos,
                          HttpServletRequest request) throws Exception{
        Result result=new Result();
        try {
            if (EmptyUtils.isEmpty(publishType)){
                result.setResultMsg("publishType不能为空");
                result.setResultCode(400);
                return result;
            }

            WcxPublishContentDto dto = new WcxPublishContentDto();
            dto.setOpenid(openid);
            dto.setName(EmojiParser.parseToAliases(name));
            dto.setPhone(phone);
            dto.setPublishType(publishType);
            String content = EmojiParser.parseToAliases(publishContent);
            dto.setPublishContent(content);
            dto.setPublishContent(SensitiveWordFilterUtil.filter(content));
            dto.setAvatarUrl(avatarUrl);
            dto.setIdentity(identity);

            List<WcxPublishFileDto> fileDto = new ArrayList<WcxPublishFileDto>();
            if (EmptyUtils.isNotEmpty(fileDtos)){
                for (String url : fileDtos){
                    WcxPublishFileDto file = new WcxPublishFileDto();
                    file.setPublishType(publishType);
                    file.setPublishUrl(url);
                    fileDto.add(file);
                }
            }
            dto.setFileDtos(fileDto);
            tygGzgH5Service.publish(dto,openid);
            result.setResultCode(200);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }
    }
    /**
     * 我的信息
     * @param request
     * @param openid
     * @return
     */
    @PostMapping(value = "/myInfo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result myInfo(HttpServletRequest request,
                         @RequestParam(value="openid",required = false) String openid,
                         @RequestParam(value = "phone",required = false) String phone
    ){
        Result result = new Result();

        try {
            WcxPublishContentDto dto = new WcxPublishContentDto();
            //dto.setOpenid(openid);
            dto.setPhone(phone);
            Map<String,Object> dataMap = tygGzgH5Service.myInfo(dto);
            result.setResultCode(200);
            result.setData(dataMap);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setResultCode(500);
            result.setResultMsg("系统服务异常，请稍后再试");
            result.setData(null);
            return result;
        }
    }
    /**
     * 删除
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result delete(@RequestParam("id") Long id,
                         HttpServletRequest request) throws Exception{
        Result result = new Result();

        try {
            WcxPublishContentDto dto = new WcxPublishContentDto();
            dto.setId(id);
            return tygGzgH5Service.delete(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setResultCode(500);
            result.setResultMsg("服务异常，请稍后再试");
            return result;
        }
    }
    /**
     * 上传附件
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception{

        Result result = new Result();

        if(null == file){
            result.setResultCode(400);
            result.setResultMsg("上传的文件为空");
            return result;
        }
        String fileName = file.getOriginalFilename();// 文件名
        String fileExtension = ""; // 文件扩展名
        fileExtension = fileName.substring(fileName.lastIndexOf("."));

        String random = RandomStringUtils.randomAlphabetic(16);
        String fileOnlyOneName = random + fileExtension;
        try {
            String url = ossUtils.uploadImageToOSS(fileOnlyOneName, new ByteArrayInputStream(file.getBytes()));
            result.setResultCode(200);
            result.setData(url);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResultMsg("服务异常，请稍后再试");
            return result;
        }
    }
    @ResponseBody
    @PostMapping("/test")
    public void test(){
        String s = SensitiveWordFilterUtil.filter("中文");
        System.out.println(s);
    }
}
