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

import com.alibaba.fastjson.JSONObject;
import com.gic.authcenter.commons.util.IgnoreLogin;
import com.gic.commons.util.GlobalVar;
import com.gic.commons.util.HttpClient;
import com.gic.haoban.common.utils.HaobanResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

@Controller
public class QywxCallbackController extends WebBaseController {

    private static final Logger logger = LoggerFactory.getLogger(QywxCallbackController.class);

    private static final String url = "http://47.94.7.218:8089/wxwork/%s" ;

    @RequestMapping("qywx-msg-notice")
    @IgnoreLogin
    public Object callback(HttpServletRequest request , HttpServletResponse response) throws IOException {
        try {
            String body = IOUtils.toString(request.getInputStream()) ;
            logger.info("企微消息下发={}",body);
            JSONObject jsonObject = JSONObject.parseObject(body) ;
            int type = jsonObject.getIntValue("type") ;
            String uuid = jsonObject.getString("uuid") ;
            JSONObject json = jsonObject.getJSONObject("json") ;
            this.post(type,uuid,json);
            Map<String,String> map = new HashMap<>() ;
            map.put("errcode","0") ;
            map.put("errmsg","ok") ;
            return map ;
        }catch (Exception e) {
            logger.info("异常",e);
        }
        return "ok" ;
    }

    private void post(int type , String uuid , JSONObject json) {
        switch (type) {
            case 100001 :
                this.v100001(uuid,json) ;
                break;
            case 100002 :
                this.v100002(uuid,json) ;
                break;
            case 100003 :
                this.v100003(uuid,json) ;
                break;
            case 100004 :
                this.v100004(uuid,json) ;
                break;
            case 100005 :
                this.close(uuid,json) ;
                break;
            case 104001 :
                this.loginSuccess(uuid,json) ;
                break;
            case 100012 :
                this.v100012(uuid,json) ;
                break;
            case 100009 :
                this.v100009(uuid,json) ;
                break;
            default:
                break;
        }
    }
    private void v100012(String uuid , JSONObject json) {
        logger.info("需要二次扫码");
    }
    private void v100009(String uuid , JSONObject json) {
        logger.info("超时未扫码关闭");
    }
    private void v100001(String uuid , JSONObject json) {
        logger.info("用户扫码={}",uuid);
    }
    private void v100004(String uuid , JSONObject json) {
        logger.info("需要输入验证码校验={}",uuid);
    }
    private void v100002(String uuid , JSONObject json) {
        logger.info("验证码校验成功返回={}",uuid);
    }
    private void loginSuccess(String uuid , JSONObject json) {
        logger.info("登录成功={}",uuid);
    }
    private void close(String uuid , JSONObject json) {
        logger.info("退出={}",uuid);
    }
    private void v100003(String uuid , JSONObject json) {
        logger.info("取消登录={}",uuid);
    }

    @RequestMapping("qywx-init")
    @IgnoreLogin
    public HaobanResponse init() {
        String callbackUrl = GlobalVar.ctxPropertiesMap.get("haoban_service_host")+"/haoban-manage3-operation-web/qywx-msg-notice" ;
        String map1 = HttpClient.sendPostJSON(String.format(url,"init") ,"{}","utf-8") ;
        logger.info(map1);
        JSONObject json = JSONObject.parseObject(map1) ;
        String uuid = json.getJSONObject("data").getString("uuid") ;
        JSONObject setCallbackUrl = new JSONObject() ;
        setCallbackUrl.put("uuid",uuid) ;
        setCallbackUrl.put("url",callbackUrl) ;
        String map2 = HttpClient.sendPostJSON(String.format(url,"SetCallbackUrl"),setCallbackUrl.toJSONString(),"utf-8") ;
        String map3 = HttpClient.sendPostJSON(String.format(url,"getQrCode"),setCallbackUrl.toJSONString(),"utf-8") ;
        List<String> list = new ArrayList<>() ;
        list.add(map1);
        list.add(map2);
        list.add(map3);
        HaobanResponse resp = new HaobanResponse() ;
        resp.setResult(list);
        return resp ;
    }

}
