package com.desfate.chart.ui.old.adapter;


import com.desfate.chart.ui.old.OTC.datas.HistoryQuoteResponse;
import com.desfate.chart.util.ByteUtils;
import com.desfate.chart.util.LogUtils;

import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
 * 历史分笔数据
 * @author yongzq
 *
 */
public class HistoryTik {
	public static final int LENGTH = 51;
	public static final int OLD_LENGTH = 40;

	// int32_t iLast; // 现价
	// double iLastVolume; // 现量
	// int32_t iBid; // 买价
	// double iBidVolume; // 买量
	// int32_t iAsk; // 卖价
	// double iAskVolume; // 卖量
	// int32_t iTime; // 时间（UTC）
	private double last;
	private double volume;
	private double bid;
	private double bidVolueme;
	private double ask;
	private double askVolume;
	private Date atTime;
	private String direction;
	private String tradeType;
	private String publicTradeType;


	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public String getTradeType() {
		return tradeType;
	}

	public void setTradeType(String tradeType) {
		this.tradeType = tradeType;
	}

	public String getPublicTradeType() {
		return publicTradeType;
	}

	public void setPublicTradeType(String publicTradeType) {
		this.publicTradeType = publicTradeType;
	}

	public double getLast() {
		return last;
	}

	public void setLast(double last) {
		this.last = last;
	}

	public double getVolume() {
		return volume;
	}

	public void setVolume(double volume) {
		this.volume = volume;
	}

	public double getBid() {
		return bid;
	}

	public void setBid(double bid) {
		this.bid = bid;
	}

	public double getBidVolueme() {
		return bidVolueme;
	}

	public void setBidVolueme(double bidVolueme) {
		this.bidVolueme = bidVolueme;
	}

	public double getAsk() {
		return ask;
	}

	public void setAsk(double ask) {
		this.ask = ask;
	}

	public double getAskVolume() {
		return askVolume;
	}

	public void setAskVolume(double askVolume) {
		this.askVolume = askVolume;
	}

	public Date getAtTime() {
		return atTime;
	}

	public void setAtTime(Date atTime) {
		this.atTime = atTime;
	}

	public static ArrayList<HistoryTik> toObject(HistoryQuoteResponse message){
		if (message == null) {
			return null;
		}
		if(message.getContent() == null || !(message.getContent().length / message.getCount() == LENGTH || message.getContent().length / message.getCount() == OLD_LENGTH)){
			return null;
		}
		byte[] content = message.getContent();
		int index = 0;
		ArrayList<HistoryTik> list = new ArrayList<HistoryTik>();
 		for(int i = 0; i < message.getCount(); i++){
			HistoryTik item = new HistoryTik();

			int last = ByteUtils.bytesToInt(content, index, ByteOrder.BIG_ENDIAN);
			index += 4;
			item.setLast(last / (Math.pow(10, message.getDecimalPlace())));

			double volume = ByteUtils.bytesToDouble(content, index, ByteOrder.LITTLE_ENDIAN);
			index += 8;
			item.setVolume(volume);

			int bid = ByteUtils.bytesToInt(content, index, ByteOrder.BIG_ENDIAN);
			index += 4;
			item.setBid(bid / (Math.pow(10, message.getDecimalPlace())));

			double bidVolume = ByteUtils.bytesToDouble(content, index, ByteOrder.LITTLE_ENDIAN);
			index += 8;
			item.setBidVolueme(bidVolume);

			int ask = ByteUtils.bytesToInt(content, index, ByteOrder.BIG_ENDIAN);
			index += 4;
			item.setAsk(ask / (Math.pow(10, message.getDecimalPlace())));

			double askVolume = ByteUtils.bytesToDouble(content, index, ByteOrder.LITTLE_ENDIAN);
			index += 8;
			item.setAskVolume(askVolume);

			long atTimeSeconds = ByteUtils.bytesToInt(content, index, ByteOrder.BIG_ENDIAN);
			index += 4;
			Calendar calendar = Calendar.getInstance();
			// 这里一定要使用long，否则int不够会导致数据溢出
            calendar.setTimeInMillis(atTimeSeconds * 1000);
            item.setAtTime(calendar.getTime());

			if (message.getContent().length / message.getCount() == LENGTH) {// 新的，加了几个字段。11
				index += 8;// 这8位暂时不处理
				// 后面加了2字节，分别解析成方向、公共交易类型
				byte[] direction = new byte[1];
				System.arraycopy(content, index, direction, 0, 1);
				item.setDirection(new String(direction));
				index += 1;
				byte[] tradeType = new byte[1];
				System.arraycopy(content, index, tradeType, 0, 1);
				item.setTradeType(new String(tradeType));
				index += 1;
				byte[] publicTradeType = new byte[1];
				System.arraycopy(content, index, publicTradeType, 0, 1);
				item.setPublicTradeType(new String(publicTradeType));
				LogUtils.e(" setPublicTradeType ", item.getPublicTradeType());
				index += 1;
			}
			list.add(item);
		}
		
		return list;
	}
}
