package com.td.WaDa.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * dapp工具类
 */
@Slf4j
@Component
public class DappUtil {

    private static String env;
    public static String getEnv() {
        return env;
    }
    @Value("${spring.profiles.active}")
    public void setEnv(String env) {
        DappUtil.env = env;
    }

    private static String waDaUrl;
    public static String getWaDaUrl() {
        return waDaUrl;
    }
    @Value("${meta-pal.url}")
    public void setWaDaUrl(String waDaUrl) {
        DappUtil.waDaUrl = waDaUrl;
    }

    /**
     * 登录验证
     */
    public static Map MetaPalLogin(String userName,String password){
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("userName", userName);
        form.add("password", password);
        //验证地址
        String DAppUrl = DappUtil.getEnv().equals("prod") ? DappUtil.getWaDaUrl()  : DappUtil.getWaDaUrl();
        DAppUrl += "/user/login";
        //log.info("获取用户直推下级的消费土地的总cnt数额：" + DAppUrl);
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //设置content-type
            MediaType type = MediaType.parseMediaType("multipart/form-data");
            headers.setContentType(type);
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //用HttpEntity封装整个请求报文

            HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
            Map response = restTemplate.postForObject(DAppUrl, files, Map.class);
            log.info("response={}", response);
            // response.get("result")
            return response;
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 获取用户直推数量（模块1：偷菜）
     * {
     *     "code": 200,
     *     "msg": "success",
     *     "data": 0
     * }
     */
    public static Integer getChildrenCount(String userId){

        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("id", userId);
        //验证地址
        //String DAppChildrenCountOnlineUrl = "https://yt.landownership.live/api/game/childrenCount";
        String DAppChildrenCountOnlineUrl = "http://192.168.20.251/api/game/childrenCount";
        String DAppChildrenCountTestUrl = "https://wp.landownership.live/api/game/childrenCount";
        String DAppUrl = DappUtil.getEnv().equals("prod") ? DAppChildrenCountOnlineUrl : DAppChildrenCountTestUrl;
        //log.info("用户直推数量地址：" + DAppUrl);
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //设置content-type
            MediaType type = MediaType.parseMediaType("multipart/form-data");
            headers.setContentType(type);
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //用HttpEntity封装整个请求报文

            HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
            Map response = restTemplate.postForObject(DAppUrl, files, Map.class);
            //log.info("response={}", response);
            return (Integer) response.get("data");
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
            //e.printStackTrace();
        }
    }

    /**
     * 根据村民ID找到所属村的cnt总支付和总兑换数量（模块4）
     * {
     *     "code": 200,
     *     "msg": "success",
     *     "data": 0
     * }
     */
    public static Map getCntPayAndSwapAmount(String userId){

        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("id", userId);
        //验证地址
        //String onlineUrl = "https://yt.landownership.live/api/game/cntPayAndSwapAmount";
        String onlineUrl = "http://192.168.20.251/api/game/cntPayAndSwapAmount";
        String testUrl = "https://wp.landownership.live/api/game/cntPayAndSwapAmount";
        String DAppUrl = DappUtil.getEnv().equals("prod") ? onlineUrl : testUrl;
        //log.info("根据村民ID找到所属村的cnt总支付和总兑换数量：" + DAppUrl);
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //设置content-type
            MediaType type = MediaType.parseMediaType("multipart/form-data");
            headers.setContentType(type);
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //用HttpEntity封装整个请求报文

            HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
            Map response = restTemplate.postForObject(DAppUrl, files, Map.class);
            log.info("response={}", response);
            return response;
        } catch (Exception e) {
            log.error(e.getMessage());
            return new HashMap<>();
            //e.printStackTrace();
        }
    }

    /**
     * 获取用户直推且有消费土地的人数量（模块7： 放牧与养殖）
     * {
     *     "code": 200,
     *     "msg": "success",
     *     "data": 0
     * }
     */
    public static Integer getChildrenBuyLandCount(String userId){

        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("id", userId);
        //验证地址
        //String DAppChildrenCountOnlineUrl = "https://yt.landownership.live/api/game/getChildrenBuyLandCount";
        String DAppChildrenCountOnlineUrl = "http://192.168.20.251/api/game/getChildrenBuyLandCount";
        String DAppChildrenCountTestUrl = "https://wp.landownership.live/api/game/getChildrenBuyLandCount";
        String DAppUrl = DappUtil.getEnv().equals("prod") ? DAppChildrenCountOnlineUrl : DAppChildrenCountTestUrl;
        //log.info("获取用户直推且有消费土地的人数量：" + DAppUrl);
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //设置content-type
            MediaType type = MediaType.parseMediaType("multipart/form-data");
            headers.setContentType(type);
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //用HttpEntity封装整个请求报文

            HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
            Map response = restTemplate.postForObject(DAppUrl, files, Map.class);
            //log.info("response={}", response);
            return (Integer) response.get("data");
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
            //e.printStackTrace();
        }
    }

    /**
     * 获取用户直推下级的消费土地的总cnt数额（模块9： 积分兑换种子）
     * {
     *     "code": 200,
     *     "msg": "success",
     *     "data": 0
     * }
     */
    public static Integer getChildrenBuyLandAmount(String userId){

        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("id", userId);
        //验证地址
        //String DAppChildrenCountOnlineUrl = "https://yt.landownership.live/api/game/getChildrenBuyLandAmount";
        String DAppChildrenCountOnlineUrl = "http://192.168.20.251/api/game/getChildrenBuyLandAmount";
        String DAppChildrenCountTestUrl = "https://wp.landownership.live/api/game/getChildrenBuyLandAmount";
        String DAppUrl = DappUtil.getEnv().equals("prod") ? DAppChildrenCountOnlineUrl : DAppChildrenCountTestUrl;
        //log.info("获取用户直推下级的消费土地的总cnt数额：" + DAppUrl);
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //设置content-type
            MediaType type = MediaType.parseMediaType("multipart/form-data");
            headers.setContentType(type);
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //用HttpEntity封装整个请求报文

            HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
            Map response = restTemplate.postForObject(DAppUrl, files, Map.class);
            //log.info("response={}", response);
            return (Integer) response.get("data");
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
            //e.printStackTrace();
        }
    }

}
