package com.zcloud.controller.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.util.Base64Utils;
import com.zcloud.util.ReturnMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;

@Controller
@RequestMapping("/app/getImages")
public class AppHandleImagesController extends BaseController {

    @Resource
    private RestTemplate restTemplate;


    @Value("${spring.profiles.active}")
    private String active;

    @RequestMapping(value = "/loginImages")
    @ResponseBody
    public ReturnMap loginImages() throws Exception {
        PageData pageData = this.getPageData();
        LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
        linkedMultiValueMap.add("secretKey", pageData.getString("secretKey"));
        RequestEntity<LinkedMultiValueMap> body = RequestEntity.post(URI.create("")).contentType(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.ALL).body(linkedMultiValueMap);
        try {
            if ("master".equals(active) || "pj".equals(active)) {
                ResponseEntity<JSONObject> exchange = restTemplate.exchange("http://47.92.102.56:8081/integrated_whb_admin/app/v1/getLoginImages", HttpMethod.POST, body, JSONObject.class);
                return ReturnMap.ok().put("data", exchange.getBody().getString("data"));
            }
            ResponseEntity<JSONObject> exchange = restTemplate.exchange("http://192.168.20.240:8501/integrated_whb_admin/app/v1/getLoginImages", HttpMethod.POST, body, JSONObject.class);
            return ReturnMap.ok().put("data", exchange.getBody().getString("data"));
        }catch (Exception e) {
            return ReturnMap.ok().put("data", "aHR0cHM6Ly9xYWFxLnFoZHNhZmV0eS5jb20vYXNzZXRzL3ZlcmlmaWNhdGlvbjEuMzRiZTE3MDcuanBnLDIxOTktMDEtMDE=");
        }

    }
}
