package com.genlot.ninglottery.game.ssq.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.ssq.bean.MultiSSQ;
import com.genlot.ninglottery.widget.BetTextView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 双色球复式投注界面
 * @date 2019/8/26
 */
public class MultiSSQView extends BetView {

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

    @OnClick({R.id.ll_betRed, R.id.ll_betBlue})
    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.tv_clear_red)
    public void clearRed(TextView view) {
        MultiSSQ bean = (MultiSSQ) mBetList.get(0);
        bean.clearRed();
        mTextBets.get(0).clearText();
        if (mCurrentRow != 0) {
            rowChange(0);
        }
        EventBus.getDefault().post(new BetChangeEvent(bean));
    }

    @OnClick(R.id.tv_clear_blue)
    public void clearBlue(TextView view) {
        MultiSSQ bean = (MultiSSQ) mBetList.get(0);
        bean.clearBlue();
        mTextBets.get(1).clearText();
        if (mCurrentRow != 1) {
            rowChange(1);
        }
        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.drawable.shape_bet_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 refreshRed() {
        mTextBets.get(0).setText(((MultiSSQ)mBetList.get(0)).mRedList);
    }

    public void refreshBlue() {
        mTextBets.get(1).setText(((MultiSSQ)mBetList.get(0)).mBlueList);
    }

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

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

    @Override
    public void clear() {
        rowChange(0);
        mBetList.get(0).clear();
        for (BetTextView view : mTextBets) {
            view.clearText();
        }
    }

    @Override
    public String[] getBetData() {
        String[] betData = new String[1];
        IBetBean bean = mBetList.get(0);
        betData[0] = bean.toBetString();
        return betData;
    }

    @Override
    public void parseBetData(String[] betData) {
        mBetList.get(0).parseBetData(betData[0]);
        refreshRed();
        refreshBlue();
    }

    @BindViews({R.id.ll_betRed, R.id.ll_betBlue})
    List<LinearLayout> mLlBets;
    @BindViews({R.id.tv_seriesRed,R.id.tv_seriesBlue})
    List<TextView> mTextSeries;
    @BindViews({R.id.tv_clear_red, R.id.tv_clear_blue})
    List<TextView> mTextClears;
    @BindViews({R.id.tv_bet_red, R.id.tv_bet_blue})
    List<BetTextView> mTextBets;
}
