package com.sptg.third.biz.qinlin.controller;

import cn.hutool.http.Header;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.UserFlagConstant;
import com.sptg.common.core.enums.Instruct;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.InstructPublishLog;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.device.api.feigns.InstructPublishLogFeign;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.estate.api.feigns.AccessRecordsFeign;
import com.sptg.third.api.beans.ApiResultBean;
import com.sptg.third.api.contants.UrlContants;
import com.sptg.third.api.entity.qinlin.*;
import com.sptg.third.api.feigns.qinlin.QLinAuthFeign;
import com.sptg.third.api.feigns.qinlin.QLinFeign;
import com.sptg.third.biz.qinlin.conf.QinLinConfig;
import com.sptg.third.biz.qinlin.service.QLinApiLogServiceInterface;
import com.sptg.third.biz.qinlin.service.QLinLoginAccountServiceInterface;
import com.sptg.third.biz.qinlin.util.QinLinHttpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zwx
 * @create 2020/12/16 15:57
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class QLinAuthController implements QLinAuthFeign {

    private final QLinLoginAccountServiceInterface qLinLoginAccountServiceInterface;
    private final RestTemplate restTemplate;


    @Override
    @PostMapping(value = "/third/qlin/auth")
    public String qLinAuth() {
        QLinLoginAccount qLinLoginAccount = this.qLinLoginAccountServiceInterface.getQLinLoginAccount();

        ResponseEntity responseEntity = null;
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.HOST, "qiotapi.qinlinkeji.com");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
        headers.add(HttpHeaders.COOKIE,"acw_tc=2760821a16113221940631027ec001b19b418287355539685439200add60cf");

        Map<String, Object> map = new HashMap<>();
        map.put("iv",qLinLoginAccount.getIv());
        map.put("loginName",qLinLoginAccount.getLoginName());
        map.put("loginPassword",qLinLoginAccount.getEncodePwd());
        map.put("scene","nc_login");

        HttpEntity requestEntity = new HttpEntity<>(JSONUtil.toJsonStr(map), headers);
        ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
        };

        URI uri = URI.create(qLinLoginAccount.getUrl());
        responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);

        Map body = (Map)responseEntity.getBody();
        Map data =(Map) body.get("data");
        Object sessionId = data.get("sessionId");
        if(sessionId != null){
            return sessionId.toString();
        }
        return null;
    }
}
