package com.bizzan.bitrade.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bizzan.bitrade.entity.MemberWallet;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class InnerInvokeUtil {
	@Value("${sms.key}")
    private String key;
	@Value("${inner.address.service.url:http://127.0.0.1:6020/address}")
	private String url;
	
	public boolean login(String username, String password) {
		String saveUrl = url + "/user/login?key="+key+"&username="+username+"&password="+password;
		String response = HttpClientUtil.get(saveUrl, null, null);
		log.info(response);
		if(response.equals("true"))
			return true;
		else
			return false;
	}
	public boolean register(String username, String password, int type) {
		String saveUrl = url + "/user/register?key="+key+"&username="+username+"&password="+password+"&type="+type;
		String response = HttpClientUtil.get(saveUrl, null, null);
		if(response.equals("true"))
			return true;
		else
			return false;
		
	}
	public boolean modifyPassword(String username, String oldPassword, String newPassword) {
		String saveUrl = url + "/user/modifyPassword?key="+key+"&username="+username+"&oldPassword="+oldPassword+"&newPassword="+newPassword;
		String response = HttpClientUtil.get(saveUrl, null, null);
		if(response.equals("true"))
			return true;
		else
			return false;
	}
	public boolean resetPassword(String account, String password, int mode) {
		String saveUrl = url + "/user/resetPassword?key="+key+"&account="+account+"&password="+password+"&mode="+mode;
		String response = HttpClientUtil.get(saveUrl, null, null);
		if(response.equals("true"))
			return true;
		else
			return false;
	}
	
	public List<MemberWallet> listWallet(Long memberId) {
		String saveUrl = url + "/asset/wallet?key="+key+"&memberId="+memberId;
		String response = HttpClientUtil.get(saveUrl, null, null);
        JSONObject json = JSON.parseObject(response, JSONObject.class);
    	List<MemberWallet> walletList;
    	if(json.get("data")!=null) {
    		walletList = JSON.parseArray(json.get("data").toString(), MemberWallet.class);
    	} else {
    		walletList = new ArrayList<MemberWallet>();
    	}
    	return walletList;
	}
	public List<MemberWallet> listAllWallet(Long memberId) {
		String saveUrl = url + "/asset/wallet/all?key="+key+"&memberId="+memberId;
		String response = HttpClientUtil.get(saveUrl, null, null);
        JSONObject json = JSON.parseObject(response, JSONObject.class);
    	List<MemberWallet> walletList;
    	if(json.get("data")!=null) {
    		walletList = JSON.parseArray(json.get("data").toString(), MemberWallet.class);
    	} else {
    		walletList = new ArrayList<MemberWallet>();
    	}
    	return walletList;
	}
	public MemberWallet fineWallet(Long memberId, String unit) {
		String saveUrl = url + "/asset/wallet/one?key="+key+"&memberId="+memberId+"&unit="+unit;
		String response = HttpClientUtil.get(saveUrl, null, null);
        JSONObject json = JSON.parseObject(response, JSONObject.class);
    	if(json.get("data")!=null) {
    		return JSON.parseObject(json.get("data").toString(), MemberWallet.class);
    	} else {
    		return null;
    	}
	}

	public void resetAddress(String unit, Long memberId, String address) {
		String saveUrl = url + "/asset/wallet/resetAddress?key="+key+"&memberId="+memberId+"&unit="+unit+"&address="+address;
        String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public void recharge(String unit, String address, BigDecimal amount) {
		String saveUrl = url + "/asset/wallet/recharge?key="+key+"&amount="+amount+"&unit="+unit+"&address="+address;
		String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public void thawBalance(String unit, Long memberId, BigDecimal amount) {
		String saveUrl = url + "/asset/wallet/thaw?key="+key+"&unit="+unit+"&memberId="+memberId+"&amount="+amount;
		String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public void withdraw(String unit, Long memberId, BigDecimal amount) {
		String saveUrl = url + "/asset/wallet/withdraw?key="+key+"&unit="+unit+"&memberId="+memberId+"&amount="+amount;
		String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public void decreaseBalance(String unit, Long memberId, BigDecimal amount) {
		String saveUrl = url + "/asset/wallet/decrease?key="+key+"&unit="+unit+"&memberId="+memberId+"&amount="+amount;
		String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public void increaseBalance(String unit, Long memberId, BigDecimal amount) {
		String saveUrl = url + "/asset/wallet/increase?key="+key+"&unit="+unit+"&memberId="+memberId+"&amount="+amount;
		String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public void initWallets(Long memberId) {
		String saveUrl = url + "/asset/wallet/initWallets?key="+key+"&memberId="+memberId;
		String response = HttpClientUtil.get(saveUrl, null, null);
		//log.info("url={}, response={}", url, response);
        logError(url, response);
	}
	
	public void logError(String url, String response) {
		JSONObject json = JSON.parseObject(response, JSONObject.class);
		if(json== null || !json.containsKey("code") || json.getInteger("code")!=0) {
			log.info("url={}, response={}", url, response);
		}
	}
	
	public boolean validateWithdrawAddress(Long memberId, String unit, Long recordId, String address) {
		String saveUrl = url + "/asset/withdraw/va720d7e?key="+key+"&memberId="+memberId+"&unit="+unit+"&recordId="+recordId+"&address="+address;
		String response = HttpClientUtil.get(saveUrl, null, null);
		JSONObject json = JSON.parseObject(response, JSONObject.class);
		return json.getInteger("code") == 0;
	}
	
	public boolean freezeBalance(String unit, Long memberId, BigDecimal amount) {
		String saveUrl = url + "/asset/wallet/freeze?key="+key+"&unit="+unit+"&memberId="+memberId+"&amount="+amount;
		String response = HttpClientUtil.get(saveUrl, null, null);
		//log.info("url={}, response={}", saveUrl, response);
		return response.equals("1");
	}
	
	public void saveWithdrawBackup(Long memberId, String unit, Long recordId, String address) {
		String saveUrl = url + "/asset/withdraw/sa720d7e?key="+key+"&memberId="+memberId+"&unit="+unit+"&recordId="+recordId+"&address="+address;
		String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
	}
	
	public JSONObject addAddress(Long memberId, String unit, String address, String remark) {
		String saveUrl = url + "/asset/address/sa720d7e?key="+key+"&memberId="+memberId+"&unit="+unit+"&address="+address+"&remark="+remark;
        String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
        return JSON.parseObject(response, JSONObject.class);
	}
	
	public JSONObject deleteAddress(Long memberId, Long id) {
		String saveUrl = url + "/asset/address/de720d7e?key="+key+"&memberId="+memberId+"&id="+id;
        String response = HttpClientUtil.get(saveUrl, null, null);
        logError(url, response);
        return JSON.parseObject(response, JSONObject.class);
	}
	
	public JSONObject getAddress(Long memberId, String unit, int pageNo, int pageSize) {
		String saveUrl = url + "/asset/address/li720d7e?key="+key+"&pageNo="+pageNo+"&pageSize="+pageSize+"&memberId="+memberId+"&unit="+unit;
        String response = HttpClientUtil.get(saveUrl, null, null);
		return JSON.parseObject(response, JSONObject.class);
	}
}
