package io.renren.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import io.renren.entity.SysBranch;
import io.renren.entity.SysPeople;
import io.renren.service.HikTest;
import io.renren.service.HikVisionService;
import io.renren.service.ISysBranchService;
import io.renren.service.ISysPeopleService;
import io.renren.util.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags="海康接口")
public class HikController{


    @Value("${artemis.url}")
    public String url;
    @Value("${artemis.appKey}")
    public String appKey;
    @Value("${artemis.appSecret}")
    public String appSecret;
    private static final String ARTEMIS_PATH = "/artemis";


    @Autowired
    private ISysBranchService branchService;

    @Autowired
    private ISysPeopleService peopleService;


    @RequestMapping(value = "enter",method = RequestMethod.GET)
    @ApiOperation("抓拍图片")
    public void hikEnter(){
        HikVisionService hikVisionService = new HikVisionService();
        hikVisionService.initEnter("172.168.200.193",123);
    }

    @RequestMapping(value = "addWhiteList",method = RequestMethod.POST)
    @ApiOperation("添加白名单")
    public void test(@RequestBody String cars){
        HikTest test = new HikTest();
        System.out.println(cars);
        List<String> carList = new ArrayList<>();
        for(String car:cars.split(",")){
            carList.add(car);
        }
        //test.init(carList);
    }

    @RequestMapping(value = "notification",method = RequestMethod.GET)
    public JSONObject notification(String AccessControllerEvent) throws Exception {
        JSONObject jsonObject = new JSONObject();

        cn.hutool.json.JSONObject jsonObject1= HttpUtil.getISAPI("192.168.19.2:8001", "admin", "Abc.1122 ", "/ISAPI/Event/notification/httpHosts/1");
        System.out.println("获取信息+++++++++++++++"+jsonObject1);
     /*   String xml= "<HttpHostNotification version=\"2.0\" xmlns=\"http://www.isapi.org/ver20/XMLSchema\">" +
                "<id>1</id>" +
                "<url>/api/remoteCheck</url>" +
                "<protocolType>HTTP</protocolType>" +
                "<parameterFormatType></parameterFormatType>" +
                "<addressingFormatType>ipaddress</addressingFormatType>" +
                "<ipAddress>"+ip+"</ipAddress>" +
                "<ipv6Address>::</ipv6Address>" +
                "<portNo>"+port+"</portNo>" +
                "<httpAuthenticationMethod>none</httpAuthenticationMethod>" +
                "<SubscribeEvent>" +
                "<heartbeat>30</heartbeat>" +
                "<pictureURLType>cloudStorageURL</pictureURLType>" +
                "<eventMode>all</eventMode>" +
                "<EventList>" +
                "<Event>" +
                "<type>AccessControllerEvent</type>" +
                "<minorAlarm></minorAlarm>" +
                "<minorException></minorException>" +
                "<minorOperation></minorOperation>" +
                "<minorEvent></minorEvent>" +
                "<pictureURLType>binary</pictureURLType>" +
                "</Event>" +
                "<Event>" +
                "<type>IDCardInfoEvent</type>" +
                "<minorEvent></minorEvent>" +
                "<pictureURLType>binary</pictureURLType>" +
                "</Event>" +
                "</EventList>" +
                "</SubscribeEvent>" +
                "</HttpHostNotification>";
        cn.hutool.json.JSONObject jsonObject2= HttpUtil.putISAPI("192.168.124.46", "admin", "SQLsql123", "/ISAPI/Event/notification/httpHosts/1",xml);
        System.out.println("返回信息+++++++++++++++"+jsonObject2);*/

        return jsonObject;
    }

    @RequestMapping(value = "orgList",method = RequestMethod.GET)
    public JSONObject orgList() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("pageNo", "1");
        paramMaps.put("pageSize", 1000 );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("list");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            SysBranch sysBranch=new SysBranch();
            sysBranch.setId(objs.get("orgIndexCode")+"");
            sysBranch.setParentId(objs.get("parentOrgIndexCode")+"");
            sysBranch.setName(objs.get("orgName")+"");
            branchService.insertSysBranch(sysBranch);
        }

        return jsonObject;
    }



    @RequestMapping(value = "personList1",method = RequestMethod.GET)
    public JSONObject personList1() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/personList";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("pageNo", "1");
        paramMaps.put("pageSize", 1000 );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("list");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            SysPeople people=new SysPeople();
            people.setName(objs.get("personName")+"");
            people.setGuid(objs.get("personId")+"");
            people.setBranchId(objs.get("orgIndexCode")+"");
            people.setGh(objs.get("jobNo")+"");
            people.setIsConsume("1");
            SysPeople peoples=peopleService.selectSysPeopleByGuid(people);
            if(peoples==null){
                peopleService.insertSysPeople(people);
            }

        }

        return jsonObject;
    }

    @RequestMapping(value = "personList2",method = RequestMethod.GET)
    public JSONObject personList2() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/personList";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("pageNo", "2");
        paramMaps.put("pageSize", 1000 );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("list");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            SysPeople people=new SysPeople();
            people.setName(objs.get("personName")+"");
            people.setGuid(objs.get("personId")+"");
            people.setBranchId(objs.get("orgIndexCode")+"");
            people.setGh(objs.get("jobNo")+"");
            people.setSex(objs.get("gender")+"");
            people.setIsConsume("1");
            SysPeople peoples=peopleService.selectSysPeopleByGuid(people);
            if(peoples==null){
                peopleService.insertSysPeople(people);
            }
        }

        return jsonObject;
    }


    @RequestMapping(value = "eventTypes",method = RequestMethod.GET)
    public JSONObject eventTypes() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventSubscriptionByEventTypes";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        String[] type=new String[198913];
        paramMaps.put("eventTypes",type);
        paramMaps.put("eventDest","http://192.168.3.254:5002/api/eventRcv");
        paramMaps.put("subType", 1 );
        String[] eventLvl=new String[2];
        paramMaps.put("eventLvl", eventLvl );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("list");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            SysPeople people=new SysPeople();
            people.setName(objs.get("personName")+"");
            people.setGuid(objs.get("personId")+"");
            people.setBranchId(objs.get("orgIndexCode")+"");
            people.setGh(objs.get("jobNo")+"");
            people.setIsConsume("1");
            peopleService.insertSysPeople(people);
        }

        return jsonObject;
    }

    @RequestMapping(value = "eventRcv",method = RequestMethod.POST)
    public JSONObject eventRcv(String Obj) throws Exception {
        JSONObject jsonObject = new JSONObject();
        System.out.println("事件接受===="+Obj);

        return jsonObject;
    }

    @RequestMapping(value = "faceList1",method = RequestMethod.GET)
    public JSONObject faceList1() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/face/get";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("dataId", "1");
        paramMaps.put("pageSize", 1000 );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("list");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            String personId=objs.get("personId")+"";
            String picUri=objs.get("picUri")+"";
            String serverIndexCode=objs.get("serverIndexCode")+"";
            picture(picUri,serverIndexCode);
            SysPeople people=new SysPeople();
            peopleService.insertSysPeople(people);
        }

        return jsonObject;
    }

    @RequestMapping(value = "faceList2",method = RequestMethod.GET)
    public JSONObject faceList2() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/face/get";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("dataId", "1001");
        paramMaps.put("pageSize", 1000 );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("list");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            String personId=objs.get("personId")+"";
            String picUri=objs.get("picUri")+"";
            String serverIndexCode=objs.get("serverIndexCode")+"";
            picture(picUri,serverIndexCode);
            /*peopleService.selectSysPeopleByGuId(personId);
            peopleService.insertSysPeople(people);*/
        }

        return jsonObject;
    }

    @RequestMapping(value = "picture",method = RequestMethod.GET)
    public JSONObject picture(String picUri,String serverIndexCode) throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/picture";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("picUri", picUri);
        paramMaps.put("serverIndexCode", serverIndexCode );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        return jsonObject;
    }

    @RequestMapping(value = "personAdd",method = RequestMethod.GET)
    public JSONObject personAdd() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/single/add\n";
        Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
        paramMaps.put("personName", "1001");
        paramMaps.put("gender", "1001");
        paramMaps.put("orgIndexCode", "1001");
        paramMaps.put("phoneNo", "1001");
        paramMaps.put("certificateNo", "11001001001100110011");
        paramMaps.put("jobNo", "111111");
        JSONArray faces=new JSONArray();
        Map<String, Object> face = new HashMap<String, Object>();
        face.put("faceData", "/9j/4AAQSkZJRgABAQEAAAAAAAD/4QBCRXhpZgAATU..");
        faces.add(face);
        paramMaps.put("faces", faces );
        String body = JSON.toJSON(paramMaps).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        String personId= (String) jsons1.get("personId");
        String faceId= (String) jsons1.get("faceId");


        return jsonObject;
    }

    @RequestMapping(value = "orgListBatch",method = RequestMethod.GET)
    public JSONObject orgListBatch() throws Exception {
        JSONObject jsonObject = new JSONObject();

        ArtemisConfig config = new ArtemisConfig();
        config.setHost(url); // 代理API网关nginx服务器ip端口
        config.setAppKey(appKey);  // 秘钥appkey
        config.setAppSecret(appSecret);// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/batch/add";
        JSONArray list1= new JSONArray();
        SysBranch branch=new SysBranch();
        branch.setIsExamine("0");
        List<SysBranch> list2= branchService.selectSysBranchList(branch);
        for(SysBranch b:list2){
            Map<String, Object> paramMaps = new HashMap<String, Object>();// post请求Form表单参数
            paramMaps.put("clientId", 123456789);
            paramMaps.put("orgIndexCode", b.getId() );
            paramMaps.put("orgName", b.getName() );
            paramMaps.put("parentIndexCode", b.getParentId());
            list1.add(paramMaps);
        }
        String body = JSON.toJSON(list1).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        String request = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        JSONObject jsons= JSONObject.parseObject(request);
        JSONObject jsons1=(JSONObject) jsons.get("data");
        JSONArray list= (JSONArray) jsons1.get("successes");
        for(Object obj:list){
            JSONObject objs= (JSONObject)obj;
            SysBranch sysBranch=new SysBranch();
            sysBranch.setId(objs.get("orgIndexCode")+"");
            sysBranch.setIsExamine("1");
            branchService.updateSysBranch(sysBranch);
        }

        return jsonObject;
    }



}
