package com.huimv.video.dhicc.test.controller;

import com.alibaba.fastjson.JSON;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo02;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo03;
import com.huimv.video.dhicc.test.constant.Constant;
import com.huimv.video.dhicc.test.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RestController
@RequestMapping("/carTest")
public class ClientCarManageControllerTest {

    @Autowired
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    @Autowired
    private OauthConfigBaseInfo02 oauthConfigBaseInfo02;
    @Autowired
    private OauthConfigBaseInfo03 oauthConfigBaseInfo03;

    //列表车辆
    @RequestMapping("/geCarList")
    public Object geCarList(@RequestParam (name = "farmId") Integer farmId,
                            @RequestParam (name = "pageNum") Integer pageNum,
                            @RequestParam (name = "pageSize") Integer pageSize) throws ClientException {

        Object action = null;
        if (farmId == 1) {
            System.out.println("曙光");
            ClientCarListRequestSg requestSg = new ClientCarListRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(pageNum);
            requestSg.setPageSize(pageSize);
            System.out.println(requestSg);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("海宁");
            ClientCarListRequestHn requestHn = new ClientCarListRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(pageNum);
            requestHn.setPageSize(pageSize);
            System.out.println(requestHn);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21){
            System.out.println("苍南");
            ClientCarListRequestCn requestCn = new ClientCarListRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(pageNum);
            requestCn.setPageSize(pageSize);
            System.out.println(requestCn);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //添加基础车辆
    @RequestMapping("/addCarBase")
    public Object addCarBase(@RequestParam (name = "carNum") String carNum,
                             @RequestParam (name = "farmId") String farmId) throws ClientException {

        Object action = null;
        if ("1".equals(farmId)) {
            System.out.println("曙光");
            ClientCarAddRequestSg requestSg = new ClientCarAddRequestSg(oauthConfigBaseInfo);
            requestSg.setCarCode(String.valueOf((Math.random() * 9 + 1) * 10000));
            requestSg.setCarNum(carNum);
            requestSg.setCarNumColor("0");
            requestSg.setCarType("1");
            requestSg.setCarBrand("-1");
            requestSg.setCarColor("0");
            requestSg.setListType(0);
            requestSg.setParkType(1);
            requestSg.setOwnerId(null);
            requestSg.setParkinglotCode("1");
            requestSg.setDeptName("技术部门");
            requestSg.setDeptId("2");
            System.out.println(requestSg);
            Object action1 = requestSg.doAction();
            action = action1;
        }  if ("22".equals(farmId)) {
            System.out.println("海宁");
            ClientCarAddRequestHn requestHn = new ClientCarAddRequestHn(oauthConfigBaseInfo02);
            requestHn.setCarCode(String.valueOf((Math.random() * 9 + 1) * 10000));
            requestHn.setCarNum(carNum);
            requestHn.setCarNumColor("0");
            requestHn.setCarType("1");
            requestHn.setCarBrand("-1");
            requestHn.setCarColor("0");
            requestHn.setListType(0);
            requestHn.setParkType(1);
            requestHn.setOwnerId(null);
            requestHn.setParkinglotCode("1");
            requestHn.setDeptName("技术部门");
            requestHn.setDeptId("2");
            System.out.println(requestHn);
            Object action1 = requestHn.doAction();
            action = action1;
        }  if ("21".equals(farmId)) {
            System.out.println("苍南");
            ClientCarAddRequestCn requestCn = new ClientCarAddRequestCn(oauthConfigBaseInfo03);
            requestCn.setCarCode(String.valueOf((Math.random() * 9 + 1) * 10000));
            requestCn.setCarNum(carNum);
            requestCn.setCarNumColor("0");
            requestCn.setCarType("1");
            requestCn.setCarBrand("-1");
            requestCn.setCarColor("0");
            requestCn.setListType(0);
            requestCn.setParkType(1);
            requestCn.setOwnerId(null);
            requestCn.setParkinglotCode("1");
            requestCn.setDeptName("技术部门");
            requestCn.setDeptId("1");
            System.out.println(requestCn);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //删除基础车辆
    @RequestMapping("/deleteFromCarBase")
    public Object deleteFromCarBase(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        Object id = params.get("id");
        Object action = null;
        if (farmId == 1) {
            System.out.println("曙光");
            ClientCarDeleteRequestSg requestSg = new ClientCarDeleteRequestSg(oauthConfigBaseInfo);
            requestSg.setId((String) id);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("海宁");
            ClientCarDeleteRequestHn requestHn = new ClientCarDeleteRequestHn(oauthConfigBaseInfo02);
            requestHn.setId((String) id);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21){
            System.out.println("苍南");
            ClientCarDeleteRequestCn requestCn = new ClientCarDeleteRequestCn(oauthConfigBaseInfo03);
            requestCn.setId((String) id);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //白名单列表
    @RequestMapping("/ShowCarWhiteList")
    public Object ShowCarWhiteList(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                            @RequestParam(name = "pageNum") String pageNum,//车辆编号
                                            @RequestParam(name = "pageSize") String pageSize //车牌号
    ) throws ClientException {
        Object action = null;
        if (farmId == 1) {
            System.out.println("曙光");
            ClientCarWhiteListRequestSg requestSg = new ClientCarWhiteListRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(pageNum);
            requestSg.setPageSize(pageSize);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("海宁");
            ClientCarWhiteListRequestHn requestHn = new ClientCarWhiteListRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(pageNum);
            requestHn.setPageSize(pageSize);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21) {
            System.out.println("苍南");
            ClientCarWhiteListRequestCn requestCn = new ClientCarWhiteListRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(pageNum);
            requestCn.setPageSize(pageSize);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;

    }


    //黑名单列表
    @RequestMapping("/ShowCarBlackList")
    public Object ShowCarBlackList(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                            @RequestParam(name = "pageNum") String pageNum,//车辆编号
                                            @RequestParam(name = "pageSize") String pageSize //车牌号
    ) throws ClientException {
        Object action = null;
        if (farmId == 1) {
            System.out.println("曙光");
            ClientCarBlackListRequestSg requestSg = new ClientCarBlackListRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(pageNum);
            requestSg.setPageSize(pageSize);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("海宁");
            ClientCarBlackListRequestHn requestHn = new ClientCarBlackListRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(pageNum);
            requestHn.setPageSize(pageSize);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21) {
            System.out.println("苍南");
            ClientCarBlackListRequestCn requestCn = new ClientCarBlackListRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(pageNum);
            requestCn.setPageSize(pageSize);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //为添加到任何名单  //draw=4是白名单    draw=5是黑名单
    @RequestMapping("/ShowUnSendCar")
    public Object ShowUnSendCar(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                         @RequestParam(name = "pageNum") String pageNum,//车辆编号
                                         @RequestParam(name = "draw") Integer draw,//车辆编号
                                         @RequestParam(name = "pageSize") String pageSize //车牌号
    ) throws ClientException {
        Object action = null;
        if (farmId == 1) {
            System.out.println("曙光");
            ClientCarAllListRequestSg requestSg = new ClientCarAllListRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(pageNum);
            requestSg.setPageSize(pageSize);
            requestSg.setDraw(String.valueOf(draw));
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("海宁");
            ClientCarAllListRequestHn requestHn = new ClientCarAllListRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(pageNum);
            requestHn.setPageSize(pageSize);
            requestHn.setDraw(String.valueOf(draw));
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21) {
            System.out.println("苍南");
            ClientCarAllListRequestCn requestCn = new ClientCarAllListRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(pageNum);
            requestCn.setPageSize(pageSize);
            requestCn.setDraw(String.valueOf(draw));
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //添加车辆进黑名单列表
    @RequestMapping("/addCarBlackList")
    public Object addCarBlackList(@RequestBody Map<String, Object> params) throws ClientException {
        Integer farmId = (int) params.get("farmId");

        List<String> carIdList = (List<String>) params.get("carIdList");
        List<Object> a = new ArrayList<>();
        for (String s : carIdList) {
            Map<String, Object> value = new HashMap<>();
            value.put("carId", s);
            value.put("validStartDay", "2021-12-22");
            value.put("validEndDay", "2036-12-22");
            value.put("deviceCodes", "1002931,1002928");
            a.add(value);
        }
        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientCarBlackAddRequestSg requestSg = new ClientCarBlackAddRequestSg(oauthConfigBaseInfo);
            requestSg.setA(a);
            System.out.println(requestSg);
            Object action1 = requestSg.doAction();
            action = action1;
        }  if (farmId == 22) {
            System.out.println("haining");
            ClientCarBlackAddRequestHn requestHn = new ClientCarBlackAddRequestHn(oauthConfigBaseInfo02);
            requestHn.setA(a);
            System.out.println(requestHn);
            Object action1 = requestHn.doAction();
            action = action1;
        }  if (farmId==21) {
            System.out.println("cangnan");
            ClientCarBlackAddRequestCn requestCn = new ClientCarBlackAddRequestCn(oauthConfigBaseInfo03);
            requestCn.setA(a);
            System.out.println(requestCn);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }

    public static void main(String[] args) {
        System.out.println((int) ((Math.random() * 9 + 1) * 10000));
    }


    //添加车辆进白名单列表
    @RequestMapping("/addCarWhiteList")
    public Object addCarWhiteList(@RequestBody Map<String, Object> params) throws ClientException {
        Integer farmId = (int) params.get("farmId");

        List<String> carIdList = (List<String>) params.get("carIdList");
        List<Object> a = new ArrayList<>();
        for (String s : carIdList) {
            Map<String, Object> value = new HashMap<>();
            value.put("carId", s);
            value.put("validStartDay", "2021-12-22");
            value.put("validEndDay", "2036-12-22");
            value.put("deviceCodes", "1002931,1002928");
            a.add(value);
        }
        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientCarWhiteAddRequestSg requestSg = new ClientCarWhiteAddRequestSg(oauthConfigBaseInfo);
            requestSg.setA(a);
            System.out.println(requestSg);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("haining");
            ClientCarWhiteAddRequestHn requestHn = new ClientCarWhiteAddRequestHn(oauthConfigBaseInfo02);
            requestHn.setA(a);
            System.out.println(requestHn);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21) {
            System.out.println("cangnan");
            ClientCarWhiteAddRequestCn requestCn = new ClientCarWhiteAddRequestCn(oauthConfigBaseInfo03);
            requestCn.setA(a);
            System.out.println(requestCn);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //从白名单删除
    @RequestMapping("/deleteFromCarWhiteList")
    public Object deleteFromCarWhiteList(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        List<String> carIdList = (List<String>) params.get("carIdList");
        System.out.println(carIdList);

        Object action=null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientCarWhiteDeleteRequestSg requestSg = new ClientCarWhiteDeleteRequestSg(oauthConfigBaseInfo);
            requestSg.setA(carIdList);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("hangning");
            ClientCarWhiteDeleteRequestHn requestHn = new ClientCarWhiteDeleteRequestHn(oauthConfigBaseInfo02);
            requestHn.setA(carIdList);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21) {
            System.out.println("cangnan");
            ClientCarWhiteDeleteRequestCn requestCn = new ClientCarWhiteDeleteRequestCn(oauthConfigBaseInfo03);
            requestCn.setA(carIdList);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }

    //从黑名单删除
    @RequestMapping("/deleteFromCarBlackList")
    public Object deleteFromCarBlackList(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        List<String> carIdList = (List<String>) params.get("carIdList");
        System.out.println(carIdList);

        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientCarBlackDeleteRequestSg requestSg = new ClientCarBlackDeleteRequestSg(oauthConfigBaseInfo);
            requestSg.setA(carIdList);
            Object action1 = requestSg.doAction();
            action = action1;
        } else if (farmId == 22) {
            System.out.println("hangning");
            ClientCarBlackDeleteRequestHn requestHn = new ClientCarBlackDeleteRequestHn(oauthConfigBaseInfo02);
            requestHn.setA(carIdList);
            Object action1 = requestHn.doAction();
            action = action1;
        } else if (farmId==21) {
            System.out.println("cangnan");
            ClientCarBlackDeleteRequestCn requestCn = new ClientCarBlackDeleteRequestCn(oauthConfigBaseInfo03);
            requestCn.setA(carIdList);
            Object action1 = requestCn.doAction();
            action = action1;
        }
        return action;
    }


    //从基础表中删除
    @RequestMapping("/deleteFromBaseCarList")
    public Object deleteFromBaseCarList(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        List<String> carIdList = (List<String>) params.get("carIdList");
        System.out.println(carIdList);

        Object action1 = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientCarBaseDeleteRequestSg requestSg = new ClientCarBaseDeleteRequestSg(oauthConfigBaseInfo);
            requestSg.setA(carIdList);
            Object action = requestSg.doAction();
            action1 = action;
        } else if (farmId == 22) {
            System.out.println("hangning");
            ClientCarBaseDeleteRequestHn requestHn = new ClientCarBaseDeleteRequestHn(oauthConfigBaseInfo02);
            requestHn.setA(carIdList);
            Object action = requestHn.doAction();
            action1 = action;
        } else if (farmId==21) {
            System.out.println("cangnan");
            ClientCarBaseDeleteRequestCn requestCn = new ClientCarBaseDeleteRequestCn(oauthConfigBaseInfo03);
            requestCn.setA(carIdList);
            Object action = requestCn.doAction();
            action1 = action;
        }
        return action1;
    }


    //过车的数据

    /**
     * 保存ntamade
     */
    @RequestMapping("/get_car_record")
    public Object getCarRecord(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId
            , @RequestParam(name = "pageNum") String pageNum
            , @RequestParam(name = "pageSize") String pageSize) throws ClientException {
        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientCarCaptureRequestSg requestSg = new ClientCarCaptureRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(pageNum);
            requestSg.setPageSize(pageSize);
            Object action1 = requestSg.doAction();
            System.out.println("执行结束");
            action = action1;
        } else if (farmId == 22) {
            System.out.println("haining");
            ClientCarCaptureRequestHn requestHn = new ClientCarCaptureRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(pageNum);
            requestHn.setPageSize(pageSize);
            Object action1 = requestHn.doAction();
            System.out.println("执行结束");
            action = action1;
        } else if (farmId==21){
            System.out.println("cangnan");
            ClientCarCaptureRequestCn requestCn = new ClientCarCaptureRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(pageNum);
            requestCn.setPageSize(pageSize);
            Object action1 = requestCn.doAction();
            System.out.println("执行结束");
            action = action1;
        }
        return action;

    }


    @RequestMapping("/get_person_detail")
    public GeneralResponse getPersonDetail(@RequestBody Map<String, Object> params) throws ClientException {

        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            //返回为空数据
            return new GeneralResponse();
        }
        String URL = "/evo-apigw/evo-accesscontrol/${version}/card/accessControl/swingCardRecord/19241";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        String URL_id = URL + params.get("id");
        //这种已经在配置文件里面安排了账号ip以及密码
        GeneralRequest generalRequest = new GeneralRequest(URL_id, Method.POST);
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        return generalResponse;
    }


}
