package com.sptg.gateway.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.dto.AccessRecordsDto;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.estate.api.feigns.AccessRecordsFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/grid/accessRecords")
public class AccessRecordsController {

    private final AccessRecordsFeign accessRecordsFeign;

    /**
     * 功能描述:抓拍记录列表查询
     *
     * @param accessRecordsDto 抓拍记录业务对象（含查询条件）
     * @param page             分页条件
     * @return ResultBean<Page < AccessRecords>>
     */
    @GetMapping(value = "/showAccessRecordsList")
    public ResultBean<Page<AccessRecords>> showAccessRecordsList(AccessRecordsDto accessRecordsDto, Page<AccessRecords> page) {
        // 暂时拿全部的数据
//        residentialDto.setOrgPosition("0/1/");
        accessRecordsDto.setPage(page);
        return this.accessRecordsFeign.showAccessRecordsList(accessRecordsDto);
    }

    @GetMapping(value = "/findAccessRecordsById/{id}")
    public ResultBean<AccessRecords> findAccessRecordsById(@PathVariable(value = "id") Long id) {
        AccessRecords accessRecords = this.accessRecordsFeign.findAccessRecordsById(id);
        Assert.notNull(accessRecords, "抓拍记录不存在");
        return new ResultBean<>(ResultEnum.SUCCESS, accessRecords);
    }

    @GetMapping(value = "/getSessionId")
    public ResultBean<String> getSessionId() {
        String sessionId = null;

        RestTemplate restTemplate = new RestTemplate();
//		HttpHeaders httpHeaders =
        ResponseEntity responseEntity = null;
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
        headers.add(HttpHeaders.CONTENT_TYPE,"application/json;charset=UTF-8");
//        headers.add(HttpHeaders.);
        headers.add(HttpHeaders.HOST, "qiotapi.qinlinkeji.com");
        headers.add("Cookie", "acw_tc=acw_tc=2760824216043762523671945eb979bec7bac43e69959c0cde59aa79eb9e0f");
        Map<String, Object> map = new HashMap<>();
//       {
//      iv: "2846983943071693"
//      key: ""
//      loginName: "银川刘浪"
//      loginPassword: "CC3EF666DD655CEB271B5BA8E1C5C04F"
//      mfaCode: ""
//      scene: "nc_login"
//      sessionId: ""
//      sig: ""
//      token: ""
//}
        map.put("iv", "1156283113535473");
        map.put("key","");
        map.put("loginName", "银川刘浪");
        map.put("loginPassword", "CB2A6259A46B0A89A4634A49D3EAD8E6");
        map.put("mfaCode","");
        map.put("sessionId","");
        map.put("scene", "nc_login");
        map.put("sig","");
        map.put("token","");


        HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(map), headers);
        ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
        };
        String url = "https://qiotapi.qinlinkeji.com/admin/user/login";
        URI uri = URI.create(url);
        responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);


        Map map1 = (Map) responseEntity.getBody();
        Map data = (Map)map1.get("data");
        sessionId = data.get("sessionId").toString();
        return new ResultBean<>(ResultEnum.SUCCESS, sessionId);
    }


}
