package com.huiju.estateagents.controller;


import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.AliOSSUtils;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.common.SMSUtils;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.service.IMiniAppService;
import com.huiju.estateagents.service.ITdMiniappTemplateTypeService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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


@RestController
@RequestMapping("/api")
public class CommonController extends BaseController {
    @Autowired
    SMSUtils smsUtils;

    @Autowired
    ITdMiniappTemplateTypeService iTdMiniappTemplateTypeService;

    @Autowired
    IMiniAppService iMiniAppService;

    /**
     * 图片
     * @param multipartFile
     * @return
     */
    @PostMapping("/{plat}/image")
    public ResponseBean uploadImage(@RequestParam("file") MultipartFile multipartFile) {
        try {
            String img = AliOSSUtils.putObject(multipartFile, CommConstant.ALIOSS_DEFAULT_UPLOAD);
            return ResponseBean.success(img);
        } catch (IOException e) {
            return ResponseBean.error("上传图片失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    @PostMapping("/admin/qrcode")
    public ResponseBean createQrCode(@RequestBody String jsonStr, HttpServletRequest request) {
        Integer orgId = getOrgId(request);

        if (StringUtils.isEmpty(jsonStr)) {
            return ResponseBean.error("参数不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        JSONObject params = JSONObject.parseObject(jsonStr);
        if (params == null) {
            return ResponseBean.error("参数是非法的 JSON 对象", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        String paramStr = params.getString("params");
        if (StringUtils.isEmpty(paramStr)) {
            ResponseBean.error("参数 params 不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        JSONObject qrObj = JSONObject.parseObject(paramStr);
        if (qrObj == null) {
            return ResponseBean.error("小程序码参数是非法", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        String scene = qrObj.getString("scene");
        String page = qrObj.getString("page");

        if (StringUtils.isEmpty(scene) || StringUtils.isEmpty(page)) {
            return ResponseBean.error("小程序码参数: scene 或者 page 不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        if (scene.indexOf("id=") == -1) {
            return ResponseBean.error("小程序码参数: 必须存在 id 参数", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        return iMiniAppService.getOrgQrCode(paramStr, orgId);
    }

    /**
     * 为适应 ANTD 特制的图片上传
     * plat 中心端传 center 管理端传 admin
     * @param multipartFile
     * @return
     */
    @PostMapping("/{plat}/antd/image")
    public Map<String, String> uploadImageForAntd(@RequestParam("file") MultipartFile multipartFile) {
        Map<String, String> result = new HashMap<>();

        try {
            String img = AliOSSUtils.putObject(multipartFile, CommConstant.ALIOSS_DEFAULT_UPLOAD);

            result.put("name", StringUtils.ifNull(multipartFile.getOriginalFilename(), multipartFile.getName()));
            result.put("status", "done");
            result.put("thumbUrl", img + "?x-oss-process=style/thumbnail");
            result.put("url", img);

        } catch (IOException e) {
            result.put("status", "error");
            result.put("message", e.getMessage());
        }

        return result;
    }

    @PostMapping("{plat}/captcha")
    public ResponseBean sendCaptcha(@RequestParam String tel) {
        boolean success = smsUtils.sendCaptcha(tel);
        if (success) {
            return ResponseBean.success("发送验证码成功");
        } else {
            return ResponseBean.error("验证码发送失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    @PostMapping("{plat}/check/captcha")
    public ResponseBean sendCaptcha(@RequestParam String tel,@RequestParam String captcha) {
        boolean success = smsUtils.checkCaptcha(tel,captcha);
        if (success) {
            return ResponseBean.success("验证码校验成功");
        } else {
            return ResponseBean.error("验证码校验失败", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    @GetMapping("/center/dict/templateType")
    public ResponseBean getAllTplTypes() {
        return ResponseBean.success(iTdMiniappTemplateTypeService.getAllTypes());
    }

    @GetMapping("/wx/awesome/dict/{typeFor}")
    public ResponseBean dicts(@PathVariable String typeFor) {

        if ("recommendcustomer".equals(typeFor)) {

            ArrayList<Map<String, String>> realtyManageType = new ArrayList<>();
            realtyManageType.add(new HashMap<String, String>(){{
                put("id", "1");
                put("name", "公寓");
            }});
            realtyManageType.add(new HashMap<String, String>(){{
                put("id", "2");
                put("name", "住宅");
            }});
            realtyManageType.add(new HashMap<String, String>(){{
                put("id", "3");
                put("name", "商铺");
            }});
            realtyManageType.add(new HashMap<String, String>(){{
                put("id", "4");
                put("name", "别墅");
            }});

            ArrayList<Map<String, String>> priceRange = new ArrayList<>();
            priceRange.add(new HashMap<String, String>(){{
                put("id", "11");
                put("name", "100万以下");
            }});
            priceRange.add(new HashMap<String, String>(){{
                put("id", "12");
                put("name", "100-200万");
            }});
            priceRange.add(new HashMap<String, String>(){{
                put("id", "13");
                put("name", "200-400万");
            }});
            priceRange.add(new HashMap<String, String>(){{
                put("id", "14");
                put("name", "400万以上");
            }});

            ArrayList<Map<String, String>> demandType = new ArrayList<>();
            demandType.add(new HashMap<String, String>(){{
                put("id", "21");
                put("name", "自主");
            }});
            demandType.add(new HashMap<String, String>(){{
                put("id", "22");
                put("name", "投资");
            }});

            return ResponseBean.success(new HashMap<String, Object>(){{
                put("realtyManageType", realtyManageType);
                put("priceRange", priceRange);
                put("demandType", demandType);
            }});
        } else {
            return ResponseBean.error("暂时不支持的字典请求", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }
    }
}
