package http.client;

import android.net.Uri;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;

import http.OkHttpUtils;
import http.vo.request.TerminalAuthReq;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Response;
import xn.xxp.room.RoomTool;
import xn.xxp.room.bean.DeviceConfig;
import xn.xxp.room.bean.LabConfig;

public final class HttpTool {

    public static Response terminalAuth(String devId) throws IOException {
        TerminalAuthReq terminalAuthReq = new TerminalAuthReq();
        terminalAuthReq.setDeviceCode(devId);
        terminalAuthReq.setCode("aio_infobord");
        DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        return OkHttpUtils.postSync(deviceConfig.getBaseUrl() + "terminal/authorize", GsonUtils.toJson(terminalAuthReq));
    }

    public static Response queryLabConfig(String devId) throws IOException {
        DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        return OkHttpUtils.getSync(deviceConfig.getBaseUrl() + "terminal/lab/getSubjectIdByNum/" + devId);
    }

    public static String checkUrl(String url) {
        try {
            String uriStr = url;
            if (!url.matches("https?://.*") && !url.matches("http?://.*")) {
                DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
                uriStr = Uri.parse(deviceConfig.getBaseUrl() + url).toString();
            }
            return uriStr;
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
            return url;
        }
    }

    public static Response getLabFaceList() {
        try {
            DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
            LabConfig labConfig = RoomTool.getInstance().labConfigDao().getLabConfig();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("subId", labConfig.getLabId());
            return OkHttpUtils.postSync(deviceConfig.getBaseUrl() + "terminal/sys/face/data/sub", jsonObject.toString());
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        return null;
    }

    public static Response getRectifyAdviceNoteView(String noticeId) throws IOException {
        DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        return OkHttpUtils.getSync(deviceConfig.getBaseUrl() + "terminal/sec/getRectifyAdviceNoteView?noticeId=" + noticeId);
    }

    public static Response getLabStatusById(String labId, String userId) throws IOException {
        DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        return OkHttpUtils.getSync(deviceConfig.getBaseUrl() + "terminal/lab/getLabStatusById?labId=" + labId + "&userId=" + userId);
    }

    public static Response heartbeat(String devId) throws IOException {
        DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        return OkHttpUtils.getSync(deviceConfig.getBaseUrl() + "terminal/device/aio/heartbeat?deviceNo=" + devId);
    }


    /**
     * 设备状态
     *
     * @param metrics json字符串 记录设备状态
     */
    public static Response deviceStatus(String devId, String metrics) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceNo", devId);
            jsonObject.put("metrics", metrics);
            DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
            return OkHttpUtils.postSync(deviceConfig.getBaseUrl() + "terminal/device/aio/upload/status", jsonObject.toString());
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        return null;
    }

    /**
     * 远程开锁发送状态
     */
    public static Response remoteUnlock(long messageId) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("messageId", messageId);
            jsonObject.put("status", 1);
            DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
            return OkHttpUtils.postSync(deviceConfig.getBaseUrl() + "terminal/lock/callback", jsonObject.toString());
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        return null;
    }

    /**
     * 查询二维码登录状态
     *
     * @param code 时间
     */
    public static Response infoBordLogin(String devId, String code) throws IOException {
        DeviceConfig deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        return OkHttpUtils.getSync(deviceConfig.getBaseUrl() + "terminal/lab/infobordLogin?macId=" + devId + "&code=" + code);
    }
}
