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 com.gic.haoban.manage.api.enums.QwOpenStepEnum;
import com.gic.haoban.manage.api.service.OpenStaffApiService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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);

    @Autowired
    private OpenStaffApiService openStaffApiService ;

    @RequestMapping("qywx-msg-notice")
    @IgnoreLogin
    public Object callback(HttpServletRequest request , HttpServletResponse response) {
        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("需要二次扫码");
        this.openStaffApiService.update2QrcodeFlag(uuid) ;
    }
    private void v100009(String uuid , JSONObject json) {
        logger.info("超时未扫码关闭");
        this.openStaffApiService.updateLoginStep(uuid, QwOpenStepEnum.step3.getStep()) ;
    }
    private void v100001(String uuid , JSONObject json) {
        logger.info("用户扫码={}",uuid);
        this.openStaffApiService.updateLoginStep(uuid,QwOpenStepEnum.step1.getStep()) ;
    }
    private void v100004(String uuid , JSONObject json) {
        logger.info("需要输入验证码校验={}",uuid);
        this.openStaffApiService.updateValiFlag(uuid) ;
    }
    private void v100002(String uuid , JSONObject json) {
        logger.info("验证码校验成功返回={}",uuid);
        this.openStaffApiService.updateLoginStep(uuid,QwOpenStepEnum.step4.getStep()) ;
    }
    private void loginSuccess(String uuid , JSONObject json) {
        logger.info("登录成功={}",uuid);
        Long qwCorpId = json.getLong("corp_id") ;
        Long qwUserid = json.getLong("user_id") ;
        String corpName = json.getString("corp_name") ;
        this.openStaffApiService.login(uuid,qwCorpId,corpName,qwUserid) ;
    }
    private void close(String uuid , JSONObject json) {
        logger.info("退出={}",uuid);
        this.openStaffApiService.logout(uuid) ;
    }
    private void v100003(String uuid , JSONObject json) {
        logger.info("取消登录={}",uuid);
        this.openStaffApiService.updateLoginStep(uuid,QwOpenStepEnum.step2.getStep()) ;
    }

}
