package com.bokecc.livemodule.live.function.lottery.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bokecc.common.utils.Tools;
import com.bokecc.livemodule.live.chat.util.DensityUtil;
import com.bokecc.sdk.mobile.live.pojo.LotteryUserInfo;

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


public class SlotMachineView extends LinearLayout {
    private TimeChooseView mTimeChooseView1;
    private TimeChooseView mTimeChooseView2;
    private TimeChooseView mTimeChooseView3;
    private TimeChooseView mTimeChooseView4;
    private TimeChooseView mTimeChooseView5;
    private int mEndCount;
    private ArrayList<LotteryUserInfo> userInfos;

    private ArrayList<TimeChooseView>mTimeChooseViews = new ArrayList<>();

    private boolean mStarted;

    public SlotMachineView(Context context) {
        super(context);
        init();
    }

    public SlotMachineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SlotMachineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void start() {
        if (userInfos == null || userInfos.size() == 0){
            return;
        }
        if (mStarted) {
            return;
        }
        mStarted = true;

        mEndCount = 0;

        mTimeChooseView1.start( 72);
        mTimeChooseView2.start(78);
        mTimeChooseView3.start(84);
        mTimeChooseView4.start(90);
        mTimeChooseView5.start(96);
    }

    public void stop(List<LotteryUserInfo> list) {
        if (!mStarted) {
            return;
        }

        if (list != null && list.size() != 0) {
            if (list.size() == 1){
                int index1 = getDataIndex(list.get(0),mTimeChooseView1);
                mTimeChooseView1.stop(index1);
                mTimeChooseView2.stop( 0);
                mTimeChooseView3.stop(1);
                mTimeChooseView4.stop(2);
                mTimeChooseView5.stop(3);

                mTimeChooseView1.setItem(list.get(0));
                mTimeChooseView2.setItem(userInfos.get(0));
                mTimeChooseView3.setItem(userInfos.get(1));
                mTimeChooseView4.setItem(userInfos.get(2));
                mTimeChooseView5.setItem(userInfos.get(3));
            } else if (list.size() == 2){
                int index1 = getDataIndex(list.get(0),mTimeChooseView1);
                int index2 = getDataIndex(list.get(1),mTimeChooseView2);
                mTimeChooseView1.stop(index1);
                mTimeChooseView2.stop( index2);
                mTimeChooseView3.stop(0);
                mTimeChooseView4.stop(1);
                mTimeChooseView5.stop(2);

                mTimeChooseView1.setItem(list.get(0));
                mTimeChooseView2.setItem(list.get(1));
                mTimeChooseView3.setItem(userInfos.get(0));
                mTimeChooseView4.setItem(userInfos.get(1));
                mTimeChooseView5.setItem(userInfos.get(2));
            }  else if (list.size() == 3){
                int index1 = getDataIndex(list.get(0),mTimeChooseView1);
                int index2 = getDataIndex(list.get(1),mTimeChooseView2);
                int index3 = getDataIndex(list.get(2),mTimeChooseView3);
                mTimeChooseView1.stop(index1);
                mTimeChooseView2.stop( index2);
                mTimeChooseView3.stop(index3);
                mTimeChooseView4.stop(0);
                mTimeChooseView5.stop(1);

                mTimeChooseView1.setItem(list.get(0));
                mTimeChooseView2.setItem(list.get(1));
                mTimeChooseView3.setItem(list.get(2));
                mTimeChooseView4.setItem(userInfos.get(0));
                mTimeChooseView5.setItem(userInfos.get(1));
            } else if (list.size() == 4){
                int index1 = getDataIndex(list.get(0),mTimeChooseView1);
                int index2 = getDataIndex(list.get(1),mTimeChooseView2);
                int index3 = getDataIndex(list.get(2),mTimeChooseView3);
                int index4 = getDataIndex(list.get(3),mTimeChooseView4);
                mTimeChooseView1.stop(index1);
                mTimeChooseView2.stop( index2);
                mTimeChooseView3.stop(index3);
                mTimeChooseView4.stop(index4);
                mTimeChooseView5.stop(0);

                mTimeChooseView1.setItem(list.get(0));
                mTimeChooseView2.setItem(list.get(1));
                mTimeChooseView3.setItem(list.get(2));
                mTimeChooseView4.setItem(list.get(3));
                mTimeChooseView5.setItem(userInfos.get(0));
            } else if (list.size() == 5){
                int index1 = getDataIndex(list.get(0),mTimeChooseView1);
                int index2 = getDataIndex(list.get(1),mTimeChooseView2);
                int index3 = getDataIndex(list.get(2),mTimeChooseView3);
                int index4 = getDataIndex(list.get(3),mTimeChooseView4);
                int index5 = getDataIndex(list.get(4),mTimeChooseView5);
                mTimeChooseView1.stop(index1);
                mTimeChooseView2.stop( index2);
                mTimeChooseView3.stop(index3);
                mTimeChooseView4.stop(index4);
                mTimeChooseView5.stop(index5);

                mTimeChooseView1.setItem(list.get(0));
                mTimeChooseView2.setItem(list.get(1));
                mTimeChooseView3.setItem(list.get(2));
                mTimeChooseView4.setItem(list.get(3));
                mTimeChooseView5.setItem(list.get(4));
            } else {
                int index1 = getDataIndex(list.get(0),mTimeChooseView1);
                int index2 = getDataIndex(list.get(1),mTimeChooseView2);
                int index3 = getDataIndex(list.get(2),mTimeChooseView3);
                int index4 = getDataIndex(list.get(3),mTimeChooseView4);
                int index5 = 4;
                Tools.log("SlotMachine stop ? index1 =" + index1 +"&index2 ="
                                + index2 + "&index3 =" + index3 + "&index4 =" + index4 + "&index5 =" + index5 );

                mTimeChooseView1.stop(index1);
                mTimeChooseView2.stop( index2);
                mTimeChooseView3.stop(index3);
                mTimeChooseView4.stop(index4);
                mTimeChooseView5.stop(index5);

                mTimeChooseView1.setItem(list.get(0));
                mTimeChooseView2.setItem(list.get(1));
                mTimeChooseView3.setItem(list.get(2));
                mTimeChooseView4.setItem(list.get(3));
                mTimeChooseView5.setItem(userInfos.get(4));
            }
        }
    }

    public void reset(){
        mStarted = false;
    }

    private synchronized int getDataIndex(LotteryUserInfo info,TimeChooseView timeChooseView){
        if (info != null && userInfos != null ){
            for (int i = 0 ; i < userInfos.size() ; i ++){
                if (!TextUtils.isEmpty(userInfos.get(i).getUserId())
                        && !TextUtils.isEmpty(info.getUserId())
                        && userInfos.get(i).getUserId().equals(info.getUserId())){
                    return i;
                }
            }

            userInfos.add(info);
            timeChooseView.setItems(userInfos);
            return userInfos.size() - 1;
//            return userInfos.indexOf(info);
        } else {
            return 0;
        }
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);

        mTimeChooseView1 = buildItem();
        mTimeChooseView2 = buildItem();
        mTimeChooseView3 = buildItem();
        mTimeChooseView4 = buildItem();
        mTimeChooseView5 = buildItem();

        mTimeChooseViews.add(mTimeChooseView1);
        mTimeChooseViews.add(mTimeChooseView2);
        mTimeChooseViews.add(mTimeChooseView3);
        mTimeChooseViews.add(mTimeChooseView4);
        mTimeChooseViews.add(mTimeChooseView5);

        addView(mTimeChooseView1, buildLayoutParam(false));
        addView(mTimeChooseView2, buildLayoutParam(false));
        addView(mTimeChooseView3, buildLayoutParam(false));
        addView(mTimeChooseView4, buildLayoutParam(false));
        addView(mTimeChooseView5, buildLayoutParam(false));

        mTimeChooseView5.setOnClickMoreListener(new TimeChooseView.OnClickMoreListener() {
            @Override
            public void clickMore() {
                if (onClickMoreListener != null){
                    onClickMoreListener.onClickMore();
                }
            }
        });
    }

    private LayoutParams buildLayoutParam(boolean first) {
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        if (!first) {
//            params.leftMargin = DensityUtil.dp2px(getContext(), 2);
//        }
        params.topMargin = DensityUtil.dp2px(getContext(), 4);
        return params;
    }

    private TimeChooseView buildItem() {
        TimeChooseView item = new TimeChooseView(getContext());
        item.setAutoScrollEndListener(new TimeChooseView.AutoScrollEndListener() {
            @Override
            public void onScrollEnd() {
                mEndCount++;
                if (mEndCount == getChildCount()) {
                    onShaiEnd();
                }
            }
        });
        return item;
    }

    private void onShaiEnd() {
        mStarted = false;
    }

    public void setData(ArrayList<LotteryUserInfo> list) {
        userInfos = list;
        mTimeChooseView1.setItems(list);
        mTimeChooseView2.setItems(list);
        mTimeChooseView3.setItems(list);
        mTimeChooseView4.setItems(list);
        mTimeChooseView5.setItems(list);
    }

//    public void setTouchable(boolean onTouchable){
//        mTimeChooseView5.setOnTouchable(onTouchable);
//    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (isTouchable){
//            return super.onInterceptTouchEvent(ev);
//        }else{
//            return true;
//        }
//    }


    private OnClickMoreListener onClickMoreListener;
    public interface OnClickMoreListener{
        void onClickMore();
    }

    public void setOnClickMoreListener(OnClickMoreListener onClickMoreListener){
        this.onClickMoreListener = onClickMoreListener;
    }
}
