package com.rc.httpcore.client;

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

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.rc.httpcore.HttpConfig;
import com.rc.httpcore.OkHttpUtils;
import com.rc.httpcore.bean.AddChemical;
import com.rc.httpcore.bean.HxpChemicalVo;
import com.rc.httpcore.vo.request.FaceCompare1Req;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;
import retrofit2.http.POST;

public final class HttpTool {

    public static String checkUrl(String url) {
        try {
            String uriStr = url;
            if (!url.matches("https?://.*") && !url.matches("http?://.*")) {
                uriStr = Uri.parse(HttpConfig.Companion.getAPI_BASE_URL() + url).toString();
            }
            return uriStr;
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
            return url;
        }
    }

    /**
     * 获取柜子信息
     */
    public static Response getCabinetList(String labId) throws IOException {
        return OkHttpUtils.getSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/getCabinetListBySub?subId=" + labId);
    }

    /**
     * 查询化学品
     */
    public static Response searchchemical(String searchValue, String chemicalLevel) throws IOException {
        return OkHttpUtils.getSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/searchChemica?searchValue=" + searchValue + "&chemicalLevel=" + chemicalLevel);
    }

    /**
     * 待入库前校验待入库的化学品是否合法
     */
    public static Response addStockCheck(HxpChemicalVo hxpChemicalVo) throws IOException {
        String json = GsonUtils.toJson(hxpChemicalVo);
        LogUtils.d(json);
        return OkHttpUtils.postSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/addStockCheck", json);
    }

    /**
     * 根据化学品code查询化学品信息
     */
    public static Response scanCodeChemical(String code, String subId) throws IOException {
        return OkHttpUtils.getSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/stockWait/scanCodeChemical?" + "label=" + code + "&subId=" + subId);
    }

    /**
     * 入库
     */
    public static Response addChemical(List<HxpChemicalVo> hxpChemicalList) throws Exception {
        String json = GsonUtils.toJson(new AddChemical(hxpChemicalList));
        LogUtils.d(json);
        return OkHttpUtils.postSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/addStock", json);
    }

    /**
     * 双人验证人脸
     *
     * @param photoFile       图片
     * @param faceCompare1Req 支持多个柜门权限验证
     */
    public static Response checkUserFaceByPic1(File photoFile, String subjectId, FaceCompare1Req faceCompare1Req) throws IOException {
        return OkHttpUtils.postFileAndJson(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/verify/checkUserFaceByPic1", photoFile, subjectId, faceCompare1Req);
    }

    /**
     * 双人验证刷卡
     */
    public static Response useCardVerify1(String card, String subjectId, List<String> doorIds) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("cardNum", card);
        map.put("subId", subjectId);
        map.put("doorIds", doorIds);
        LogUtils.json(map);
        return OkHttpUtils.postSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/verify/useCardVerify1", GsonUtils.toJson(map));
    }

    /**
     * 双人验证扫码
     * 小程序扫码后查询小程序扫码登录状态
     */
    public static Response aioScanLogin1(String time, String subjectId, List<String> doorIds) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("code", time);
        map.put("subId", subjectId);
        map.put("doorIds", doorIds);
        LogUtils.json(map);
        return OkHttpUtils.postSync(HttpConfig.Companion.getAPI_BASE_URL() + "chemical/aio/verify/aioScanLogin1", GsonUtils.toJson(map));
    }

}
