package com.genlot.ninglottery.game.d3.betview;

import android.content.Context;
import android.graphics.Color;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.genlot.ninglottery.R;
import com.genlot.ninglottery.event.BetChangeEvent;
import com.genlot.ninglottery.game.bet.BetView;
import com.genlot.ninglottery.game.bet.IBetBean;
import com.genlot.ninglottery.game.d3.bean.MultiD3;
import com.genlot.ninglottery.widget.BetTextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 3D直选复式投注界面
 * @date 2019/8/28
 */
public class MultiD3View extends BetView {

    public MultiD3View(Context context) {
        super(context);
        initData();
    }

    @OnClick({R.id.ll_bet1, R.id.ll_bet2, R.id.ll_bet3})
    public void betClicked(LinearLayout view) {
        int newRow = mLlBets.indexOf(view);
        if (mCurrentRow != newRow) {
            rowChange(newRow);
            EventBus.getDefault().post(new BetChangeEvent(mBetList.get(0)));
        }
    }

    @OnClick({R.id.rv_clear1, R.id.rv_clear2, R.id.rv_clear3})
    public void clearOneBet(TextView view) {
        int newRow = mTextClears.indexOf(view);
        MultiD3 bean = (MultiD3) mBetList.get(0);
        if (newRow == 0) {
            bean.mList1.clear();
        } else if (newRow == 1) {
            bean.mList2.clear();
        } else {
            bean.mList3.clear();
        }
        mTextBets.get(newRow).clearText();
        if (mCurrentRow != newRow) {
            rowChange(newRow);
        }
        EventBus.getDefault().post(new BetChangeEvent(bean));
    }

    @Override
    public void rowChange(int newRow) {
        mCurrentRow = newRow;
        for (int i = 0; i < mLlBets.size(); i++) {
            if (i == newRow) {
                mLlBets.get(i).setBackground(mContext.getDrawable(R.mipmap.bet_single_select));
                mTextSeries.get(i).setBackground(mContext.getDrawable(R.mipmap.bet_series_select));
                mTextSeries.get(i).setTextColor(Color.WHITE);
                mTextClears.get(i).setBackground(mContext.getDrawable(R.mipmap.clear_single_select));
            } else {
                mLlBets.get(i).setBackground(null);
                mTextSeries.get(i).setBackground(mContext.getDrawable(R.mipmap.bet_series_normal));
                mTextSeries.get(i).setTextColor(Color.parseColor("#FF706969"));
                mTextClears.get(i).setBackground(mContext.getDrawable(R.mipmap.clear_single_normal));
            }
        }
    }

    public void refreshBai() {
        mTextBets.get(0).setText(((MultiD3)mBetList.get(0)).mList1);
    }

    public void refreshShi() {
        mTextBets.get(1).setText(((MultiD3)mBetList.get(0)).mList2);
    }

    public void refreshGe() {
        mTextBets.get(2).setText(((MultiD3)mBetList.get(0)).mList3);
    }

    @Override
    public int getLayoutID() {
        return R.layout.view_multi_d3;
    }

    @Override
    protected void initData() {
        IBetBean bean = new MultiD3();
        mBetList.add(bean);
    }

    @Override
    public void clear() {
        rowChange(0);
        mBetList.get(0).clear();
        for (int i = 0; i < 3; i++) {
            mTextBets.get(i).clearText();
        }
    }

    @Override
    public String[] getBetData() {
        List<String> betList = new ArrayList<>();
        for (IBetBean bean : mBetList) {
            if (bean.isFull()) {
                betList.add(bean.toBetString());
            }
        }
        return betList.toArray(new String[0]);
    }

    @Override
    public void parseBetData(String[] betData) {
        mBetList.get(0).parseBetData(betData[0]);
        refreshBai();
        refreshShi();
        refreshGe();
    }

    @BindViews({R.id.ll_bet1, R.id.ll_bet2, R.id.ll_bet3})
    List<LinearLayout> mLlBets;
    @BindViews({R.id.tv_series1, R.id.tv_series2, R.id.tv_series3})
    List<TextView> mTextSeries;
    @BindViews({R.id.rv_clear1, R.id.rv_clear2, R.id.rv_clear3})
    List<TextView> mTextClears;
    @BindViews({R.id.tv_bet_info1, R.id.tv_bet_info2, R.id.tv_bet_info3})
    List<BetTextView> mTextBets;
}
