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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.desfate.chart.R;
import com.desfate.chart.data.ChartMarketGoodsDatas;
import com.desfate.chart.ui.old.OTC.datas.BidQuoteDay;
import com.desfate.chart.ui.old.OTC.listener.MessageEvent;
import com.desfate.chart.ui.old.OTC.listener.MessageingManager;
import com.desfate.chart.ui.old.OTC.listener.OnMessageListener;
import com.desfate.chart.ui.old.ParamConfigData;

import java.util.ArrayList;

/**
 * 挂牌转让
 * 独自维护生命周期
 *
 * @author Administrator
 */
public class AttornDataControl implements OnMessageListener {
    private Context mContext;
    private View view;
//    private GoodsInfoAndQuotes goodsInfoAndQuotes;
    private TextView nh_sale;
    private TextView nh_buy;
    // 买卖五档
    private TextView mtp_value_sale_five;
    private TextView mtp_sum_sale_five;
    private TextView mtp_value_sale_four;
    private TextView mtp_sum_sale_four;
    private TextView mtp_value_sale_three;
    private TextView mtp_sum_sale_three;
    private TextView mtp_value_sale_two;
    private TextView mtp_sum_sale_two;
    private TextView mtp_value_sale_one;
    private TextView mtp_sum_sale_one;

    private TextView mtp_value_buy_one;
    private TextView mtp_sum_buy_one;
    private TextView mtp_value_buy_two;
    private TextView mtp_sum_buy_two;
    private TextView mtp_value_buy_three;
    private TextView mtp_sum_buy_three;
    private TextView mtp_value_buy_four;
    private TextView mtp_sum_buy_four;
    private TextView mtp_value_buy_five;
    private TextView mtp_sum_buy_five;
    private TextView loading_view_text;

    private AttornDataControl() {
    }

    private void initViews() {
        view = LayoutInflater.from(mContext).inflate(R.layout.chart_attorn, null);

        nh_sale = view.findViewById(R.id.nh_sale);
        nh_buy = view.findViewById(R.id.nh_buy);

        mtp_value_sale_five = view.findViewById(R.id.mtp_value_sale_five);
        mtp_sum_sale_five = view.findViewById(R.id.mtp_sum_sale_five);

        mtp_value_sale_four = view.findViewById(R.id.mtp_value_sale_four);
        mtp_sum_sale_four = view.findViewById(R.id.mtp_sum_sale_four);

        mtp_value_sale_three = view.findViewById(R.id.mtp_value_sale_three);
        mtp_sum_sale_three = view.findViewById(R.id.mtp_sum_sale_three);

        mtp_value_sale_two = view.findViewById(R.id.mtp_value_sale_two);
        mtp_sum_sale_two = view.findViewById(R.id.mtp_sum_sale_two);

        mtp_value_sale_one = view.findViewById(R.id.mtp_value_sale_one);
        mtp_sum_sale_one = view.findViewById(R.id.mtp_sum_sale_one);

        mtp_value_buy_one = view.findViewById(R.id.mtp_value_buy_one);
        mtp_sum_buy_one = view.findViewById(R.id.mtp_sum_buy_one);

        mtp_value_buy_two = view.findViewById(R.id.mtp_value_buy_two);
        mtp_sum_buy_two = view.findViewById(R.id.mtp_sum_buy_two);

        mtp_value_buy_three = view.findViewById(R.id.mtp_value_buy_three);
        mtp_sum_buy_three = view.findViewById(R.id.mtp_sum_buy_three);

        mtp_value_buy_four = view.findViewById(R.id.mtp_value_buy_four);
        mtp_sum_buy_four = view.findViewById(R.id.mtp_sum_buy_four);

        mtp_value_buy_five = view.findViewById(R.id.mtp_value_buy_five);
        mtp_sum_buy_five = view.findViewById(R.id.mtp_sum_buy_five);
    }

    public View getView() {
        return view;
    }

    /**
     * 实例化
     *
     * @param context
     * @param goodsid
     */
    public AttornDataControl(Context context, String goodsid) {
        MessageingManager.getInstance().regiter(this);
        this.mContext = context;
        initViews();
        fetchForRefresh(goodsid);
    }

    /**
     * 实例化
     *
     * @param context
     * @param goodsInfoAndQuotes
     */
    public AttornDataControl(Context context, ChartMarketGoodsDatas goodsInfoAndQuotes) {
        MessageingManager.getInstance().regiter(this);
        this.mContext = context;
        initViews();
//        this.goodsInfoAndQuotes = goodsInfoAndQuotes;
        if (supportAttorn()) {
//            fetchForRefresh(goodsInfoAndQuotes.getGoodsInfo().getGoodsId() + "");
        }
    }


    /**
     * 是否支持挂牌转让
     *
     * @return
     */
    public boolean supportAttorn() {
        return ParamConfigData.QUOTED_TRANSFER;
//		if (goodsInfoAndQuotes == null) {
//			return false;
//		}
//		return goodsInfoAndQuotes.getTradeMode() == MTPEnums.BIDDING &&
//				goodsInfoAndQuotes.getGoodsInfo().getMoneyMode() == MTPEnums.MONEYMODE_PAY &&
//				MTPApplication.getInstance().isBidAttornMode(goodsInfoAndQuotes.getGoodsInfo().getGoodsId());
    }

    public void destory() {
        MessageingManager.getInstance().unRegiter(this);
    }

    public void clearAndGone() {
        mtp_value_sale_five.setText("");
        mtp_sum_sale_five.setText("");
        mtp_value_sale_four.setText("");
        mtp_sum_sale_four.setText("");
        mtp_value_sale_three.setText("");
        mtp_sum_sale_three.setText("");
        mtp_value_sale_two.setText("");
        mtp_sum_sale_two.setText("");
        mtp_value_sale_one.setText("");
        mtp_sum_sale_one.setText("");

        mtp_value_buy_one.setText("");
        mtp_sum_buy_one.setText("");
        mtp_value_buy_two.setText("");
        mtp_sum_buy_two.setText("");
        mtp_value_buy_three.setText("");
        mtp_sum_buy_three.setText("");
        mtp_value_buy_four.setText("");
        mtp_sum_buy_four.setText("");
        mtp_value_buy_five.setText("");
        mtp_sum_buy_five.setText("");

        mtp_value_sale_five.setVisibility(View.GONE);
        mtp_sum_sale_five.setVisibility(View.GONE);
        mtp_value_sale_four.setVisibility(View.GONE);
        mtp_sum_sale_four.setVisibility(View.GONE);
        mtp_value_sale_three.setVisibility(View.GONE);
        mtp_sum_sale_three.setVisibility(View.GONE);
        mtp_value_sale_two.setVisibility(View.GONE);
        mtp_sum_sale_two.setVisibility(View.GONE);
        mtp_value_sale_one.setVisibility(View.GONE);
        mtp_sum_sale_one.setVisibility(View.GONE);

        mtp_value_buy_one.setVisibility(View.GONE);
        mtp_sum_buy_one.setVisibility(View.GONE);
        mtp_value_buy_two.setVisibility(View.GONE);
        mtp_sum_buy_two.setVisibility(View.GONE);
        mtp_value_buy_three.setVisibility(View.GONE);
        mtp_sum_buy_three.setVisibility(View.GONE);
        mtp_value_buy_four.setVisibility(View.GONE);
        mtp_sum_buy_four.setVisibility(View.GONE);
        mtp_value_buy_five.setVisibility(View.GONE);
        mtp_sum_buy_five.setVisibility(View.GONE);
    }

    /**
     * 重新获取数据
     */
    public void fetchForRefresh(String goodsid) {
//		try {
//			SendMessageController.getCommonMode().queryAskStoreOrder(goodsid, MTPApplication.getInstance().getTransferHubCenter());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		try {
//			SendMessageController.getCommonMode().queryBidStoreOrder(goodsid, MTPApplication.getInstance().getTransferHubCenter());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
        clearAndGone();
        dealBidQueue();
        dealAskQueue();

    }

    private synchronized void dealBidQueue() {

        BidQuoteDay quoteTik =/* (BidQuoteDay) goodsInfoAndQuotes.getQuoteDay()*/null;
        if (quoteTik == null) {
            return;
        }
        String bidQueueInfo = quoteTik.getBidQueueInfo();

//		String bidQueueInfo = "万俊甫,55023,12182038123810238,10,25.22;";
        if (bidQueueInfo != null && !bidQueueInfo.equals("")) {// 买委托列队
//            boolean ret = QuoteCollention.getInstance().addBidStoreOrderDatas(bidQueueInfo);// 添加到本地列队中...
            boolean ret = false;// 添加到本地列队中...
            if (ret) {
                // 请求
                try {
//					SendMessageController.getCommonMode().queryBidStoreOrder(String.valueOf(goodsInfoAndQuotes.getGoodsInfo().getGoodsId()),
//							MTPApplication.getInstance().getTransferHubCenter());
//					Toast.makeText(mContext, "AttornDataControl.行情中有该账号的数据，重新请求", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                clearDown();
                reFreshBidData();
            }
        } else {
            clearDown();
//            QuoteCollention.getInstance().getBidStoreOrderDatas().clear();
            reFreshBidData();
        }
    }

    private synchronized void dealAskQueue() {
//        BidQuoteDay quoteTik = (BidQuoteDay) goodsInfoAndQuotes.getQuoteDay();
        BidQuoteDay quoteTik = null;
        if (quoteTik == null) {
            return;
        }
        String askQueueInfo = quoteTik.getAskQueueInfo();
//		String askQueueInfo = "万俊甫1,55022,12182038123810233,11,25.11;";
        if (askQueueInfo != null && !askQueueInfo.equals("")) {// 卖委托列队
            boolean ret = /*QuoteCollention.getInstance().addAskStoreOrderDatas(askQueueInfo)*/false;
            if (ret) {
                // 请求
                try {
//					SendMessageController.getCommonMode().queryAskStoreOrder(String.valueOf(goodsInfoAndQuotes.getGoodsInfo().getGoodsId()),
//							MTPApplication.getInstance().getTransferHubCenter());
//					Toast.makeText(mContext, "Attorn ask.行情中有该账号的数据，重新请求", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                clearUp();
                reFreshAskData();
            }
        } else {
//            QuoteCollention.getInstance().getAskStoreOrderDatas().clear();
            clearUp();
            reFreshAskData();
        }
    }

    @Override
    public void onMessageEventThread(MessageEvent messageEvent) {
        if (0x41 == messageEvent.getFunCode()) {
            boolean canGo = false;
            if (messageEvent.getMessageInfo() instanceof ArrayList) {
                ArrayList<String> arrayList = (ArrayList) messageEvent.getMessageInfo();
                for (int a = 0; a < arrayList.size(); a++) {
                    String goodsCode = "";
                    if (arrayList.get(a).contains("_")) {
                        goodsCode = arrayList.get(a).split("_")[1];
//                        if (goodsInfoAndQuotes.getGoodsInfo().getGoodsCode().equals(goodsCode)) {
//                            canGo = true;
//                            break;
//                        }
                    }
                }
            }
            if (canGo) {
                dealBidQueue();
                dealAskQueue();
            }

        } else if ("QueryAskStoreOrder".equals(messageEvent.getQueryCommonId())) {// 市场上全部的竞价卖持仓
            clearUp();
            reFreshAskData();
        } else if ("QueryBidStoreOrder".equals(messageEvent.getQueryCommonId())) {// 市场上全部的竞价买持仓
            clearDown();
            reFreshBidData();
        }
    }

    private void reFreshBidData() {
//        ArrayList<ListingBidStoreOrderData> arrayList = QuoteCollention.getInstance().getBidStoreOrderDatas();
//        ListingBidStoreOrderData askStoreOrderData = null;
        String name = "";
        String price = "";
        String count = "";
        int decimalPlace = 0;

//			if (arrayList.size() == 0) {
//				nh_buy.setVisibility(View.GONE);
//			}
//        for (int i = 0; i < arrayList.size(); i++) {
//            if (i >= 5) {
//                return;
//            }
//            askStoreOrderData = arrayList.get(i);
//            decimalPlace = goodsInfoAndQuotes.getGoodsInfo().getDecimalPlace();
//            name = askStoreOrderData.getAccountname();
//            if (name != null && name.length() > 1) {
//                name = MyUtils.makeNameSecret(name);
//            }
//
//            price = MathUtil.roundNum(askStoreOrderData.getOrderprice(), decimalPlace);
//
//            count = askStoreOrderData.getEnableqty();
//            switch (i + 5) {
//                case 0:
//                    showItem(mtp_value_sale_five, mtp_sum_sale_five, name, price, count);
//                    break;
//                case 1:
//                    showItem(mtp_value_sale_four, mtp_sum_sale_four, name, price, count);
//                    break;
//                case 2:
//                    showItem(mtp_value_sale_three, mtp_sum_sale_three, name, price, count);
//                    break;
//                case 3:
//                    showItem(mtp_value_sale_two, mtp_sum_sale_two, name, price, count);
//                    break;
//                case 4:
//                    showItem(mtp_value_sale_one, mtp_sum_sale_one, name, price, count);
//                    break;
//                case 5:
//                    showItem(mtp_value_buy_one, mtp_sum_buy_one, name, price, count);
//                    break;
//                case 6:
//                    showItem(mtp_value_buy_two, mtp_sum_buy_two, name, price, count);
//                    break;
//                case 7:
//                    showItem(mtp_value_buy_three, mtp_sum_buy_three, name, price, count);
//                    break;
//                case 8:
//                    showItem(mtp_value_buy_four, mtp_sum_buy_four, name, price, count);
//                    break;
//                case 9:
//                    showItem(mtp_value_buy_five, mtp_sum_buy_five, name, price, count);
//                    break;
//                default:
//                    break;
//            }
//        }
    }

    /**
     * 隐藏名字后面的文字
     *
     * @return 哇哈哈->哇**
     */
    public static String makeNameSecret(String name) {
        if (name == null || "".equals(name)) {
            return "";
        }
        if (name.length() == 1) {
            return name;
        }
        char tem = '*';
        char[] value = name.toCharArray();
        for (int a = 0; a < value.length; a++) {
            if (a != 0) {
                value[a] = tem;
            }
        }
        name = new String(value);
        return name;
    }


    private void clearUp() {
        mtp_value_sale_five.setVisibility(View.GONE);
        mtp_sum_sale_five.setVisibility(View.GONE);
        mtp_value_sale_four.setVisibility(View.GONE);
        mtp_sum_sale_four.setVisibility(View.GONE);
        mtp_value_sale_three.setVisibility(View.GONE);
        mtp_sum_sale_three.setVisibility(View.GONE);
        mtp_value_sale_two.setVisibility(View.GONE);
        mtp_sum_sale_two.setVisibility(View.GONE);
        mtp_value_sale_one.setVisibility(View.GONE);
        mtp_sum_sale_one.setVisibility(View.GONE);

    }

    private void clearDown() {
        mtp_value_buy_one.setVisibility(View.GONE);
        mtp_sum_buy_one.setVisibility(View.GONE);
        mtp_value_buy_two.setVisibility(View.GONE);
        mtp_sum_buy_two.setVisibility(View.GONE);
        mtp_value_buy_three.setVisibility(View.GONE);
        mtp_sum_buy_three.setVisibility(View.GONE);
        mtp_value_buy_four.setVisibility(View.GONE);
        mtp_sum_buy_four.setVisibility(View.GONE);
        mtp_value_buy_five.setVisibility(View.GONE);
        mtp_sum_buy_five.setVisibility(View.GONE);
    }


    private void reFreshAskData() {
//        ArrayList<ListingAskStoreOrderData> arrayList = QuoteCollention.getInstance().getAskStoreOrderDatas();
//        ListingAskStoreOrderData askStoreOrderData = null;
        String name = "";
        String price = "";
        String count = "";
        int decimalPlace = 0;
//			if (arrayList.size() == 0) {
//				nh_sale.setVisibility(View.GONE);
//			}
//        for (int i = 0; i < arrayList.size(); i++) {
//            if (i >= 5) {// 最多就只显示10个持仓
//                return;
//            }
//            askStoreOrderData = arrayList.get(i);
//            decimalPlace = goodsInfoAndQuotes.getGoodsInfo().getDecimalPlace();
//            name = askStoreOrderData.getAccountname();
//            if (name != null && name.length() > 1) {
//                name = MyUtils.makeNameSecret(name);
//            }
//
//            price = MathUtil.roundNum(askStoreOrderData.getOrderprice(), decimalPlace);
//
//            count = askStoreOrderData.getEnableqty();
//            switch (i) {
//                case 0:
//                    showItem(mtp_value_sale_five, mtp_sum_sale_five, name, price, count);
//                    break;
//                case 1:
//                    showItem(mtp_value_sale_four, mtp_sum_sale_four, name, price, count);
//                    break;
//                case 2:
//                    showItem(mtp_value_sale_three, mtp_sum_sale_three, name, price, count);
//                    break;
//                case 3:
//                    showItem(mtp_value_sale_two, mtp_sum_sale_two, name, price, count);
//                    break;
//                case 4:
//                    showItem(mtp_value_sale_one, mtp_sum_sale_one, name, price, count);
//                    break;
//                case 5:
//                    showItem(mtp_value_buy_one, mtp_sum_buy_one, name, price, count);
//                    break;
//                case 6:
//                    showItem(mtp_value_buy_two, mtp_sum_buy_two, name, price, count);
//                    break;
//                case 7:
//                    showItem(mtp_value_buy_three, mtp_sum_buy_three, name, price, count);
//                    break;
//                case 8:
//                    showItem(mtp_value_buy_four, mtp_sum_buy_four, name, price, count);
//                    break;
//                case 9:
//                    showItem(mtp_value_buy_five, mtp_sum_buy_five, name, price, count);
//                    break;
//                default:
//                    break;
//            }
//        }
    }

    private void showItem(TextView namePrice, TextView count, String name, String price, String countValue) {
        namePrice.setVisibility(View.VISIBLE);
        count.setVisibility(View.VISIBLE);

        namePrice.setText(name + "  " + price);
//        count.setText(MathUtil.roundNoNum(countValue, 0));
    }

}
