package com.genlot.ninglottery.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.game.bet.BetNumber;
import com.genlot.ninglottery.widget.decoration.GridItemDecoration;

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

/**
 * @author ZhuYongKang
 * @description 投注信息TextView
 * @date 2019/8/26
 */
public class BetTextView extends LinearLayout {

    private Context mContext;
    private View mView;
    private RecyclerView mRecycler;
    private List<BetNumber> mList;
    private BetNumberAdapter mAdapter;

    public BetTextView(Context context) {
        this(context, null);
    }

    public BetTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BetTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BetTextView);
        int rowCount = ta.getInt(R.styleable.BetTextView_span_count, 7);
        int spaceH = ta.getInt(R.styleable.BetTextView_space_h, 7);
        int spaceV = ta.getInt(R.styleable.BetTextView_space_v, 10);
        ta.recycle();
        mContext = context;
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_bet_text, this, true);
        mRecycler = mView.findViewById(R.id.rv_bet);
        mRecycler.setLayoutManager(new GridLayoutManager(mContext, rowCount));
        mRecycler.addItemDecoration(new GridItemDecoration(spaceV, spaceH));
        mList = new ArrayList<>();
        mAdapter = new BetNumberAdapter(R.layout.view_bet_text_item, mList);
        mRecycler.setAdapter(mAdapter);
    }

    /**
     * 修改单行数量 间距
     * @param count 数量
     * @param spaceH  水平间距
     * @param spaceV  垂直间距
     */
    public void changeBetSize(int count, int spaceH, int spaceV) {
        mRecycler.setLayoutManager(new GridLayoutManager(mContext, count));
        mRecycler.removeItemDecorationAt(0);
        mRecycler.addItemDecoration(new GridItemDecoration(spaceV, spaceH));
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    public void setText(List<BetNumber> list) {
        mList =list;
        mAdapter.replaceData(mList);
    }

    public void clearText() {
        mList.clear();
        mAdapter.replaceData(mList);
    }

    class BetNumberAdapter extends BaseQuickAdapter<BetNumber, BaseViewHolder>{

        BetNumberAdapter(int layoutResId, @Nullable List<BetNumber> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, BetNumber item) {
            if (item == null) {
                return;
            }
            helper.setText(R.id.tv_number, item.getNumber());
            if (item.isRed()) {
                if (item.getType() == BetNumber.DAN) {
                    helper.setBackgroundRes(R.id.tv_number, R.mipmap.bet_number_dan);
                } else {
                    helper.setBackgroundRes(R.id.tv_number, R.mipmap.bet_number_red);
                }
            } else {
                helper.setBackgroundRes(R.id.tv_number, R.mipmap.bet_number_blue);
            }
        }
    }
}
