package com.tykj.dev.blockcha.subject.service.impl;

import com.tykj.dev.blockcha.conf.BcUrl;
import com.tykj.dev.blockcha.subject.entity.*;
import com.tykj.dev.blockcha.subject.service.BlockChainUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

/**
 * 步骤：
 * 1.构建请求参数
 * 2.通过rest_template获取数据
 * 测试
 * @author LJJ cnljj1995@gmail.com
 * on 2020-08-13
 */
@Slf4j
@Component
public class BlockChainUtilImpl implements BlockChainUtil {


    private  String SECRET_KEY = "3mdeM1dw2FfHrd5h6WAuhB3WYdDG6ZAgz96CcM1nzxbiYA8QaaZHW75Je9xKixTTD2rwujyb1gVUNPYm5W6FPZQ8";
    private  String SECRET_ID = "3yXRRiL9xsLfbt2kv7qRH2so6ChJX3rFkay7Njcsxfbp";
    //测试环境

//    public  String SECRET_KEY;
//    public  String SECRET_ID;
    //测试环境
//    public String SECRET_KEY = "3RJWStDCXN7mqJyZAB8rF59f87arGShdk9aKyDN3wKbzq9JnaXom1Hpe6mHT1k2yx9zfE7VFWidqwuxyT7DtPjCs";
//    public String SECRET_ID = "JAjQyTZdR8bmbHaU6iiFGvrCuUik4cG4U4ostk8KKrbS";

    private final static String BLOCK_CHAIN_URL = "http://10.153.4.12:8076";
    private final static String BLOCK_CHAIN_URL2 = "http://10.153.8.10:8076";
    private final static String BLOCK_CHAIN_URL3 = "http://10.153.8.11:8076";
    private final static String BLOCK_CHAIN_URL4 = "http://10.153.110.10:8076";
    private final static String BLOCK_CHAIN_URL5 = "http://10.153.6.11:8076";

//    @Value("${useBlockcha:fasle")
//    private String useBlockcha;

    @Override
    public BcStatus getStatus() {

        SortedMap<Object, Object> request = getRequest();
        request.put("apiSignature", ApiSignatureUtil.getApiSignature(request, SECRET_KEY));

        return new RestTemplate().postForObject(
                BLOCK_CHAIN_URL + BcUrl.GET_STATUS,
                request,
                BcStatus.class);
    }

    @Override
    public BcRegister subRegister(String subName) {

        SortedMap<Object, Object> request = getRequest();
        request.put("subName", subName);
        request.put("apiSignature", ApiSignatureUtil.getApiSignature(request, SECRET_KEY));

        return new RestTemplate().postForObject(
                BLOCK_CHAIN_URL + BcUrl.SUB_CODE_REGISTER,
                request,
                BcRegister.class
        );
    }

    @Override
    public BcText sendText(Integer index,Integer subCode, String content) {

        SortedMap<Object, Object> request = getRequest();
        request.put("content", content);
        request.put("subCode", subCode);
        request.put("apiSignature", ApiSignatureUtil.getApiSignature(request, SECRET_KEY));

//        switch (index){
//            case 1:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL + BcUrl.SEND_TEXT,
//                        request,
//                        BcText.class
//                );
//            case 2:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL2 + BcUrl.SEND_TEXT,
//                        request,
//                        BcText.class
//                );
//            case 3:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL3 + BcUrl.SEND_TEXT,
//                        request,
//                        BcText.class
//                );
//            case 4:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL4 + BcUrl.SEND_TEXT,
//                        request,
//                        BcText.class
//                );
//            case 5:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL5 + BcUrl.SEND_TEXT,
//                        request,
//                        BcText.class
//                );
//        }
//        return new RestTemplate().postForObject(
//                BLOCK_CHAIN_URL + BcUrl.SEND_TEXT,
//                request,
//                BcText.class
//        );
        return new BcText().toData();
    }

    @Override
    public BcText sendText(Integer subCode, String content) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", content);
        request.put("subCode", subCode);
        request.put("apiSignature", ApiSignatureUtil.getApiSignature(request, SECRET_KEY));
        return new BcText().toData();
    }

//    public static void main(String[] args) {
//        new BlockChainUtilImpl().sendHash(1,"阿斯顿好看");
//    }


    @Override
    public BcHash sendHash(Integer index,Integer subCode, String content) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", HexUtil.hex64(content));
        request.put("subCode", subCode);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        log.info("apiSignature:{}",s);
        request.put("apiSignature", s);

        return new BcHash().toDate();
    }

    @Override
    public BcHash sendHash(Integer subCode, String content) {
//        if ("true".equals(this.useBlockcha)) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", HexUtil.hex64(content));
        request.put("subCode", subCode);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        log.info("apiSignature:{}",s);
        request.put("apiSignature", s);

        return new BcHash().toDate();
//        }
//        else {
//            return new BcHash(0, "", new BcHashData("000"));
//        }
    }

    @Override
    public BcRecord fetchRecord(String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("recordID", recordId);
        request.put("apiSignature", ApiSignatureUtil.getApiSignature(request, SECRET_KEY));
        return new RestTemplate().postForObject(
                BLOCK_CHAIN_URL + BcUrl.FETCH_RECORD,
                request,
                BcRecord.class
        );
    }

    @Override
    public BcHistory fetchHistory(Map<String, Object> map) {

        SortedMap<Object, Object> request = getRequest();
        map.forEach(request::put);
        request.put("apiSignature", ApiSignatureUtil.getApiSignature(request, SECRET_KEY));
        return new RestTemplate().postForObject(
                BLOCK_CHAIN_URL + BcUrl.FETCH_HISTORY,
                request,
                BcHistory.class
        );
    }

    @Override
    public BcText appendText(Integer index,String content, String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", content);
        request.put("recordID", recordId);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        request.put("apiSignature", s);


//        switch (index){
//            case 1:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL + BcUrl.APPEND_TEXT,
//                        request,
//                        BcText.class);
//            case 2:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL2 + BcUrl.APPEND_TEXT,
//                        request,
//                        BcText.class);
//            case 3:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL3 + BcUrl.APPEND_TEXT,
//                        request,
//                        BcText.class);
//            case 4:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL4 + BcUrl.APPEND_TEXT,
//                        request,
//                        BcText.class);
//            case 5:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL5 + BcUrl.APPEND_TEXT,
//                        request,
//                        BcText.class);
//        }
//
//
//        return new RestTemplate().postForObject(
//                BLOCK_CHAIN_URL + BcUrl.APPEND_TEXT,
//                request,
//                BcText.class);
        return new BcText().toData();
    }

    @Override
    public BcText appendText(String content, String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", content);
        request.put("recordID", recordId);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        request.put("apiSignature", s);

        return new BcText().toData();
    }

    @Override
    public BcHash appendHash(Integer index,String content, String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", HexUtil.hex64(content));
        request.put("recordID", recordId);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        request.put("apiSignature", s);


//        switch (index){
//            case 1:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL + BcUrl.APPEND_HASH,
//                        request,
//                        BcHash.class
//                );
//            case 2:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL2 + BcUrl.APPEND_HASH,
//                        request,
//                        BcHash.class
//                );
//            case 3:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL3 + BcUrl.APPEND_HASH,
//                        request,
//                        BcHash.class
//                );
//            case 4:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL4 + BcUrl.APPEND_HASH,
//                        request,
//                        BcHash.class
//                );
//            case 5:
//                return new RestTemplate().postForObject(
//                        BLOCK_CHAIN_URL5 + BcUrl.APPEND_HASH,
//                        request,
//                        BcHash.class
//                );
//        }
//
//        return new RestTemplate().postForObject(
//                BLOCK_CHAIN_URL + BcUrl.APPEND_HASH,
//                request,
//                BcHash.class
//        );
        return new BcHash().toDate();

    }

    @Override
    public BcHash appendHash(String content, String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", HexUtil.hex64(content));
        request.put("recordID", recordId);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        request.put("apiSignature", s);

//        return new RestTemplate().postForObject(
//                BLOCK_CHAIN_URL + BcUrl.APPEND_HASH,
//                request,
//                BcHash.class
//        );
        return new BcHash().toDate();
    }

    @Override
    public BcVerify verifyHash(String content, String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", HexUtil.hex64(content));
        request.put("recordID", recordId);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        request.put("apiSignature", s);

        return new RestTemplate().postForObject(
                BLOCK_CHAIN_URL + BcUrl.VERIFY_HASH,
                request,
                BcVerify.class
        );
    }

    @Override
    public BcVerify verifyText(String content, String recordId) {
        SortedMap<Object, Object> request = getRequest();
        request.put("content", content);
        request.put("recordID", recordId);
        String s = ApiSignatureUtil.getApiSignature(request, SECRET_KEY);
        request.put("apiSignature", s);

        return new RestTemplate().postForObject(
                BLOCK_CHAIN_URL + BcUrl.VERIFY_TEXT,
                request,
                BcVerify.class
        );
    }

    @Override
    public String verifyText1(String id, String key) {
        SECRET_KEY=key;
        SECRET_ID=id;
        return SECRET_KEY;
    }

    /**
     * 存储通用参数
     *
     * @return request
     */
    private SortedMap<Object, Object> getRequest() {
        long timestamp = System.currentTimeMillis() / 1000;
        String nonce = UUID.randomUUID().toString().trim().replaceAll("-", "");
        SortedMap<Object, Object> request = new TreeMap<Object, Object>();
        request.put("timestamp", timestamp);
        request.put("nonce", nonce);
        request.put("secretID", SECRET_ID);
        return request;
    }

}
