package com.genlot.ninglottery.game.common;

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.IBetBean;
import com.genlot.ninglottery.game.bet.BetView;
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 单式投注界面基类
 * @date 2019/8/26
 */
public class SingleView extends BetView {

    private Class mType;

    public SingleView(Context context,Class type) {
        super(context);
        this.mType = type;
        initData();
    }

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

    @OnClick({R.id.tv_random1, R.id.tv_random2, R.id.tv_random3, R.id.tv_random4, R.id.tv_random5})
    public void randomOneBet(TextView view) {
        int newRow = mTextRandoms.indexOf(view);
        IBetBean betBean = mBetList.get(newRow);
        betBean.random();
        mTextBets.get(newRow).setText(betBean.getAllNumberList());
        if (mCurrentRow != newRow) {
            rowChange(newRow);
        }
        EventBus.getDefault().post(new BetChangeEvent(betBean));
    }

    @OnClick({R.id.rv_clear1, R.id.rv_clear2, R.id.rv_clear3, R.id.rv_clear4, R.id.rv_clear5})
    public void clearOneBet(TextView view) {
        int newRow = mTextClears.indexOf(view);
        IBetBean bean = mBetList.get(newRow);
        bean.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);
                mTextRandoms.get(i).setBackground(mContext.getDrawable(R.mipmap.random_single_select));
                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"));
                mTextRandoms.get(i).setBackground(mContext.getDrawable(R.mipmap.random_single_normal));
                mTextClears.get(i).setBackground(mContext.getDrawable(R.mipmap.clear_single_normal));
            }
        }
    }

    public void refreshRow(int row) {
        mTextBets.get(row).setText(mBetList.get(row).getAllNumberList());
    }

    public IBetBean getCurrentBet() {
        return mBetList.get(mCurrentRow);
    }

    @Override
    public void clear() {
        rowChange(0);
        for (int i = 0; i < 5; i++) {
            mBetList.get(i).clear();
            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) {
        for (int i = 0; i < 5; i++) {
            mBetList.get(i).parseBetData(betData[i]);
            mTextBets.get(i).setText(mBetList.get(i).getAllNumberList());
        }
    }

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

    @Override
    protected void initData() {
        try {
            for (int i = 0; i < 5; i++) {
                IBetBean bean = (IBetBean) mType.newInstance();
                mBetList.add(bean);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public void changeBetSize(int count, int spaceH, int spaceV) {
        for (BetTextView betTextView : mTextBets) {
            betTextView.changeBetSize(count, spaceH, spaceV);
        }
    }

    @BindViews({R.id.ll_bet1, R.id.ll_bet2, R.id.ll_bet3, R.id.ll_bet4, R.id.ll_bet5})
    List<LinearLayout> mLlBets;
    @BindViews({R.id.tv_series1, R.id.tv_series2, R.id.tv_series3, R.id.tv_series4, R.id.tv_series5})
    List<TextView> mTextSeries;
    @BindViews({R.id.tv_random1, R.id.tv_random2, R.id.tv_random3, R.id.tv_random4, R.id.tv_random5})
    List<TextView> mTextRandoms;
    @BindViews({R.id.rv_clear1, R.id.rv_clear2, R.id.rv_clear3, R.id.rv_clear4, R.id.rv_clear5})
    List<TextView> mTextClears;
    @BindViews({R.id.tv_bet_info1, R.id.tv_bet_info2, R.id.tv_bet_info3, R.id.tv_bet_info4, R.id.tv_bet_info5})
    List<BetTextView> mTextBets;

}
