package com.desfate.chart.ui.old.OTC.controls;

import android.content.Context;

import com.desfate.chart.R;
import com.desfate.chart.ui.old.OTC.datas.FiveDataItemData;
import com.desfate.chart.util.MathUtil;

import java.util.ArrayList;

/**
 * 五档行情
 *
 * @author Administrator
 */
public class FiveDataControl {
    private ArrayList<FiveDataItemData> fiveDataItemDatas = null;
    private ArrayList<FiveDataItemData> dataItemDatas = null;
//    private GoodsInfoAndQuotes goodsInfoAndQuotes;
    private String surgedLimit = null;
    private String declineLimit = null;
    private Context context;

    private int decimalPlace = 0;

    public FiveDataControl(long goodsId, Context cntext) {
        this.context = cntext;
//        goodsInfoAndQuotes = GlobalDataCollention.getInstance().getmGoodsInfoClass().get((int) goodsId);
//        BaseBidControl control = new BaseBidControl(goodsInfoAndQuotes);
//        decimalPlace = control.getPriceUnit();
//        double price = control.getSurgedLimitPrice();
//        surgedLimit = price <= 0 ? "--"
//                : MathUtil.roundNum(price + "", goodsInfoAndQuotes.getGoodsInfo().getDecimalPlace());
//        price = control.getDeclineLimitPrice();
//        declineLimit = price <= 0 ? "--"
//                : MathUtil.roundNum(price + "", goodsInfoAndQuotes.getGoodsInfo().getDecimalPlace());
        fiveDataItemDatas = new ArrayList<FiveDataItemData>();
        dataItemDatas = new ArrayList<FiveDataItemData>();
        setDatas(goodsId);
    }

    private String roundValume(String num) {
        if (Double.parseDouble(num) >= 1000000) {
            return MathUtil.roundNum((Double.parseDouble(num) / 10000) + "", 0) + context.getResources().getString(R.string.ten_thousand);
        } else if (Double.parseDouble(num) >= 100000) {
            return MathUtil.roundNum((Double.parseDouble(num) / 10000) + "", 1) + context.getResources().getString(R.string.ten_thousand);
        } else if (Double.parseDouble(num) >= 10000) {
            return MathUtil.roundNum((Double.parseDouble(num) / 10000) + "", 2) + context.getResources().getString(R.string.ten_thousand);
        } else {
            return num;
        }
    }

    /**
     * 赋值数据
     */
    public void setDatas(long goodsId) {
        try {
//            BidQuoteDay bidQuoteDay = ((BidQuoteDay) GlobalDataCollention.getInstance().getmGoodsInfoClass()
//                    .get((int) goodsId).getQuoteDay());
//            if (bidQuoteDay == null) {
//                init();
//                return;
//            }
//            if (!fiveDataItemDatas.isEmpty()) {
//                fiveDataItemDatas.clear();
//            }
//            String price;
//            if (Double.parseDouble(bidQuoteDay.getAsk5()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getAsk5(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_ask5) + price, roundValume(bidQuoteDay.getAskVolume5())));
//            if (Double.parseDouble(bidQuoteDay.getAsk4()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getAsk4(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_ask4) + price, roundValume(bidQuoteDay.getAskVolume4())));
//            if (Double.parseDouble(bidQuoteDay.getAsk3()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getAsk3(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_ask3) + price, roundValume(bidQuoteDay.getAskVolume3())));
//            if (Double.parseDouble(bidQuoteDay.getAsk2()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getAsk2(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_ask2) + price, roundValume(bidQuoteDay.getAskVolume2())));
//            if (Double.parseDouble(bidQuoteDay.getAsk()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getAsk(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_ask1) + price, roundValume(bidQuoteDay.getAskVolume())));
//            if (Double.parseDouble(bidQuoteDay.getBid()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getBid(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_bid1) + price, roundValume(bidQuoteDay.getBidVolume())));
//            if (Double.parseDouble(bidQuoteDay.getBid2()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getBid2(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_bid2) + price, roundValume(bidQuoteDay.getBidVolume2())));
//            if (Double.parseDouble(bidQuoteDay.getBid3()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getBid3(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_bid3) + price, roundValume(bidQuoteDay.getBidVolume3())));
//            if (Double.parseDouble(bidQuoteDay.getBid4()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getBid4(), decimalPlace);
//            }
//            fiveDataItemDatas
//                    .add(makeItemData(this.context.getString(R.string.chart_bid4) + price, roundValume(bidQuoteDay.getBidVolume4())));
//            if (Double.parseDouble(bidQuoteDay.getBid5()) == 0) {
//                price = "0";
//            } else {
//                price = MathUtil.roundNum(bidQuoteDay.getBid5(), decimalPlace);
//            }

            dataItemDatas.add(makeItemData(this.context.getString(R.string.surged_limit), surgedLimit));
            dataItemDatas.add(makeItemData(this.context.getString(R.string.decline_limit), declineLimit));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        if (!fiveDataItemDatas.isEmpty()) {
            fiveDataItemDatas.clear();
        }

        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_ask5) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_ask4) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_ask3) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_ask2) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_ask1) + 0, "0"));

        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_bid1) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_bid2) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_bid3) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_bid4) + 0, "0"));
        fiveDataItemDatas.add(makeItemData(this.context.getString(R.string.chart_bid5) + 0, "0"));

        dataItemDatas.add(makeItemData(this.context.getString(R.string.surged_limit), surgedLimit));
        dataItemDatas.add(makeItemData(this.context.getString(R.string.decline_limit), declineLimit));
    }

    private FiveDataItemData makeItemData(String value, String sum) {
        FiveDataItemData fiveDataItemData = new FiveDataItemData();
        fiveDataItemData.setValue(value == null ? "0" : value);
        fiveDataItemData.setSum(sum);
        return fiveDataItemData;
    }

    public ArrayList<FiveDataItemData> getItems() {
        return fiveDataItemDatas;
    }

    /**
     * 涨停涨跌
     *
     * @return
     */
    public ArrayList<FiveDataItemData> getDataItemDatas() {
        return dataItemDatas;
    }

}
