package com.usoftchina.uas.office.dingtalk.manage.controller;

import com.usoftchina.dingtalk.sdk.config.DingTalkProperties;
import com.usoftchina.dingtalk.sdk.util.UrlUtils;
import com.usoftchina.uas.office.dingtalk.manage.entity.DingTalkAgent;
import com.usoftchina.uas.office.dingtalk.manage.service.DingTalkAgentService;
import com.usoftchina.uas.office.dto.Result;
import com.usoftchina.uas.office.entity.DataCenter;
import com.usoftchina.uas.office.jdbc.DataSourceHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yingp
 * @date 2020/2/16
 */
@RequestMapping(path = "/mgm")
@RestController
public class DingTalkAgentController {

    @Autowired
    private DingTalkAgentService agentService;

    @Autowired
    private DingTalkProperties properties;

    @GetMapping(path = "/agent")
    public Result getAgentList() {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            return Result.success(agentService.findAll());
        } finally {
            DataSourceHolder.clear();
        }
    }

    @PostMapping(path = "/agent")
    public Result saveAgent(DingTalkAgent agent) {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            agentService.save(agent);
            return Result.success();
        } finally {
            DataSourceHolder.clear();
        }
    }

    @DeleteMapping(path = "/agent")
    public Result deleteAgent(String code) {
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            agentService.delete(code);
            return Result.success();
        } finally {
            DataSourceHolder.clear();
        }
    }

    /**
     * 获取用于从钉钉跳转到具体应用的授权url
     *
     * @param code
     * @return
     */
    @GetMapping(path = "/agent/auth_url")
    public Result getAgentAuthUrl(String code) throws Exception{
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            return Result.error("数据中心未设置");
        }
        if (null == dataCenter.getOuterUrl()) {
            return Result.error("外网地址未设置");
        }
        if (null == properties.getCorpId()) {
            return Result.error("企业微信ID未设置");
        }
        try {
            DataSourceHolder.set(dataCenter);
            DingTalkAgent agent = agentService.findByCode(code);
            if (null == agent.getOuterUrl()) {
                return Result.error("应用外网地址未设置");
            }
            String authUrl = UrlUtils.generateAuthUrl(properties.getCorpId(), dataCenter.getUsername(), code,
                    agent.getOuterUrl(), dataCenter.getOuterUrl() + "authorize", "/");
            return Result.success(authUrl);
        } finally {
            DataSourceHolder.clear();
        }
    }
}
