package easydo.technology.service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import easydo.technology.components.JdbcClient;
import easydo.technology.model.SysUser;
import easydo.technology.util.HttpClientUtil;
import easydo.technology.utils.LocalDateUtil;
import easydo.technology.utils.RedisUtils;
import lombok.extern.java.Log;

@Log
@Component
public class OAService {
    @Value("${OA.host}")
    private String host;
    @Value("${OA.getTokenPath}")
    private String getTokenPath;
    @Value("${OA.username}")
    private String username;
    @Value("${OA.password}")
    private String password;
    @Value("${OA.getUsernamePath}")
    String oaUsernamePath;
    @Value("${OA.getUserInfoPath}")
    String oaUserInfoPath;
    @Value("${OA.sendTodoPath}")
    String oaSendTodoPath;
    @Value("${OA.updateTodoPath}")
    String oaUpdateTodoPath;
    @Value("${OA.registerCode}")
    String registerCode;
    @Value("${PURCHASE.requestUrl}")
    String requestUrl;
    @Value("${OA.sendMessagePath}")
    String oaSendMessagePath;
    @Resource
    RedisUtils redisUtils;
    @Resource
    JdbcClient jdbcClient;

    public String getGetToken() {
        String url = host + getTokenPath;
        JSONObject object = new JSONObject();
        object.put("userName", username);
        object.put("password", password);

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "查询OAtoken的req========>" + object.toJSONString() + ",url========>" + url);
        JSONObject resObject = HttpClientUtil.sendPOST(url, object.toJSONString());
        log.info(uuid + "查询OAtoken的res========>" + resObject.toJSONString());
        return resObject.getString("id");
    }

    public String getUsername(String ticket) {
        String getLoginNameUrl = host + oaUsernamePath + ticket;

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "查询OA username的url========>" + getLoginNameUrl);
        String username = HttpClientUtil.sendOaGET_USERNAME(getLoginNameUrl);
        log.info(uuid + "查询OA username的ticket====>" + ticket + ",res======>" + username);
        return HttpClientUtil.sendOaGET_USERNAME(getLoginNameUrl);
    }

    public String getTelNumber(String username) {
        String token = getGetToken();
        String getLoginNameUrl = host + oaUserInfoPath + token;
        Map<String, String> header = new HashMap<>();
        header.put("token", token);

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "查询OA telNumber的url========>" + getLoginNameUrl);
        JSONObject resObj = HttpClientUtil.sendGETWithHeader_Obj(getLoginNameUrl, header);
        log.info(uuid + "查询OA telNumber的username======>" + username + ",res======>" + resObj.toJSONString());
        return resObj.getString("telNumber");
    }

    public JSONObject sendSupplierTodo(String taskId, String sysUserId, String businessId, String title)
            throws Exception {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(Long.valueOf(sysUserId));
        sysUser = jdbcClient.getJdbcModel(sysUser);
        String creationDate = LocalDateUtil.localDateTimeFormat(LocalDateTime.now(), "yyyy-MM-dd HH:mm");

        String token = getGetToken();
        String sendTodoUrl = host + oaSendTodoPath + token;
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        JSONObject object = new JSONObject();
        object.put("registerCode", registerCode);
        object.put("taskId", taskId);
        object.put("title", title);
        object.put("senderName", sysUser.getNickName());
        object.put("classify", "招采系统");
        object.put("contentType", "招采系统");
        object.put("state", 0);
        object.put("thirdSenderId", "");
        object.put("thirdReceiverId", "");
        object.put("creationDate", creationDate);
        object.put("content", "");
        object.put("h5url", "");
        object.put("url",
                requestUrl + "type=supplier_evaluate&path=supplier-evaluation-detail&businessId=" + businessId);
        object.put("noneBindingSender", sysUser.getUsername());
        object.put("noneBindingReceiver", sysUser.getUsername());

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "生成OA待办req========>" + object.toJSONString() + ",url========>" + sendTodoUrl);
        JSONObject resObj = HttpClientUtil.sendPOSTWithHeader(sendTodoUrl, object.toJSONString(), header);
        log.info(uuid + "生成OA待办res========>" + resObj.toJSONString());

        resObj.put("url", object.getString("url"));
        return resObj;
    }

    public Boolean updateSupplierTodo(String taskId, Integer state, Integer subState)
            throws Exception {
        String token = getGetToken();
        String updateTodoUrl = host + oaUpdateTodoPath + token;
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        JSONObject object = new JSONObject();
        object = new JSONObject();
        object.put("registerCode", registerCode);
        object.put("taskId", taskId);
        object.put("state", state);
        object.put("subState", subState);

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "更新OA待办req========>" + object.toJSONString() + ",url========>" + updateTodoUrl);
        JSONObject resObj = HttpClientUtil.sendPOSTWithHeader(updateTodoUrl, object.toJSONString(), header);
        log.info(uuid + "更新OA待办res========>" + resObj.toJSONString());
        return resObj.getBoolean("success");
    }

    public Boolean sendMessage(String messageId, String sendUserId, String receiveUserId, String message,String url)
            throws Exception {
        SysUser sendUser = new SysUser();
        sendUser.setUserId(Long.valueOf(sendUserId));
        sendUser = jdbcClient.getJdbcModel(sendUser);
        SysUser receiveUser = new SysUser();
        receiveUser.setUserId(Long.valueOf(receiveUserId));
        receiveUser = jdbcClient.getJdbcModel(receiveUser);
        String creationDate = LocalDateUtil.localDateTimeFormat(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");

        String token = getGetToken();
        String sendMessageUrl = host + oaSendMessagePath + token;
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        JSONObject object = new JSONObject();
        object.put("thirdpartyRegisterCode", registerCode);
        object.put("thirdpartyMessageId", messageId);
        object.put("messageContent", message);
        object.put("thirdpartyReceiverId", receiveUser.getUserId());
        object.put("creation_date", creationDate);
        object.put("messageURL", url);

        object.put("noneBindingSender", sendUser.getUsername());
        object.put("noneBindingReceiver", receiveUser.getUsername());

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "推送OA消息req========>" + object.toJSONString() + ",url========>" + sendMessageUrl);
        JSONObject resObj = HttpClientUtil.sendPOSTWithHeader(sendMessageUrl, object.toJSONString(), header);
        log.info(uuid + "推送OA消息res========>" + resObj.toJSONString());
        return resObj.getBoolean("success");
    }
}
