package com.sifang.modules.esign.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sifang.modules.contract.service.ContractTemplateService;
import com.sifang.modules.esign.cn.tsign.hz.comm.EsignHttpResponse;
import com.sifang.modules.esign.query.AuthCallBackRes;
import com.sifang.modules.esign.query.FlowPathEndReq;
import com.sifang.modules.esign.query.OrgSealAuthVo;
import com.sifang.modules.esign.query.SignCallBackRes;
import com.sifang.modules.esign.service.ESignAuthService;
import com.sifang.modules.esign.service.ESignService;
import com.sifang.modules.esign.vo.FileVo;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "${adminPath}/main/sign")
public class ContractSignController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ContractTemplateService templateService;

    @Autowired
    ESignService eSignService;

    /**
     * 签约回调
    */
    @ResponseBody
    @RequestMapping(value = "companySignCallBack" )
    public Result companySignCallBack(@RequestBody FlowPathEndReq backRes, HttpServletRequest request) {
       // logger.info(">>>>>>>>>>>>>companySignCallBack  backRes:{} ", backRes);
       // SignCallBackRes backRes = JSONObject.parseObject(json, SignCallBackRes.class);
        logger.info("action================ {}",request.getParameter("action"));
        logger.info(">>>>>>>>>>>>>>>companySignCallBack backRes:{} ,{},{},{},{}", backRes.getAction(),backRes.getSignFlowId());
        try {
            eSignService.signContractCallBack(backRes);
        } catch (BusinessException c) {
            logger.error(">>>>>>>>>>>>>>>>>>companySignCallBack c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>>companySignCallBack e:{}", e);
            return Result.fail("");
        }
        return Result.ok();
    }

    /**
     * 签约回调（华融农户合同 签署回调）
     */
    @ResponseBody
    @RequestMapping(value = "signTentContractCallback")
    public Result signTentContractCallback(@RequestBody String json) {
        logger.info(">>>>>>>>>>>>>signTentContractCallback  json:{} ", json);
        SignCallBackRes backRes = JSONObject.parseObject(json, SignCallBackRes.class);
        logger.info(">>>>>>>>>>>>>>>signTentContractCallback backRes:{}", backRes.toString());
        try {
            eSignService.signTentContractCallback(backRes);
        } catch (BusinessException c) {
            logger.error(">>>>>>>>>>>>>>>>>>signTentContractCallback c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>>signTentContractCallback e:{}", e);
            return Result.fail("");
        }
        return Result.ok();
    }

    //项目公司认证
    @ResponseBody
    @RequestMapping(value = "authConfirmCallBack")
    public Result authConfirmCallBack(@RequestBody String json){
        logger.info(">>>>>>>>>>>>>authConfirmCallBack  json:{} ", json);
        AuthCallBackRes backRes= JSONObject.parseObject(json, AuthCallBackRes.class);
        try {
           eSignService.authConfirmCallBack(backRes);
        } catch (BusinessException c) {
            logger.error(">>>>>>>>>>>>>>>>>>authConfirmCallBack c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>>authConfirmCallBack e:{}", e);
            return Result.fail("");
        }
        return Result.ok();
    }

    //农户授权签约回调   /main/sign/authTenantCallBack
    @ResponseBody
    @RequestMapping(value = "authTenantCallBack")
    public Result authTenantCallBack(@RequestBody String json){
        logger.info(">>>>>>>>>>>>>authTenantCallBack json:{}",json);
        SignCallBackRes backRes = JSONObject.parseObject(json, SignCallBackRes.class);
        logger.info(">>>>>>>>>>>>>>>authTenantCallBack backRes:{}", backRes.toString());
        try {
            eSignService.authTenantCallBack(backRes);
        } catch (BusinessException c) {
            logger.error(">>>>>>>>>>>>>>>>>>authTenantCallBack c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>>authTenantCallBack e:{}", e);
            return Result.fail("");
        }
        return Result.ok();
    }

    //印章 跨企业授权  回调
    @ResponseBody
    @RequestMapping(value = "orgSealExternalAuthCallBack")
    public Result orgSealExternalAuthCallBack(@RequestBody String json){
        logger.info(">>>>>>>>>>>>>orgSealExternalAuthCallBack json:{}",json);
        OrgSealAuthVo orgSealAuthVo=JSONObject.parseObject(json,OrgSealAuthVo.class);
        logger.info(">>>>>>>>>>>>>>>orgSealExternalAuthCallBack backRes:{}", orgSealAuthVo.toString());
        try {
            eSignService.orgSealExternalAuthCallBack(orgSealAuthVo);
        } catch (BusinessException c) {
            logger.error(">>>>>>>>>>>>>>>>>>orgSealExternalAuthCallBack c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>>orgSealExternalAuthCallBack e:{}", e);
            return Result.fail("");
        }
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "findjssdk")
    public Object findjssdk(){
        try {
            String response =  ESignAuthService.findjssdk();
           return response;
        } catch (BusinessException c) {
            logger.error(">>>>>>>>>>>>>>>>>>authTenantCallBack c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>>authTenantCallBack e:{}", e);
            return Result.fail("");
        }
       // return Result.ok();
    }

}
