package com.duben.microtribe.ui.widgets.luckymonkeypanel;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.duben.microtribe.R;
import com.duben.microtribe.mvp.model.NineListBean;

import java.util.List;

public class LuckyMonkeyPanelView2 extends FrameLayout {

    private ImageView bg_1;
    private ImageView bg_2;

    private PanelItemView2 itemView1, itemView2, itemView3,
            itemView4, itemView6,
            itemView7, itemView8, itemView9;

    private ItemView[] itemViewArr = new ItemView[8];
    private int currentIndex = 0;
    private int currentTotal = 0;
    private int stayIndex = 0;

    private boolean isMarqueeRunning = false;
    private boolean isGameRunning = false;
    private boolean isTryToStop = false;

    private static final int DEFAULT_SPEED = 150;
    private static final int MIN_SPEED = 50;
    private int currentSpeed = DEFAULT_SPEED;

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

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

    public LuckyMonkeyPanelView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.view_lucky_mokey_panel2, this);
        setupView();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startMarquee();
    }

    @Override
    protected void onDetachedFromWindow() {
        stopMarquee();
        super.onDetachedFromWindow();
    }

    private void setupView() {
        bg_1 = (ImageView) findViewById(R.id.bg_1);
        bg_2 = (ImageView) findViewById(R.id.bg_2);
        itemView1 = (PanelItemView2) findViewById(R.id.item1);
        itemView2 = (PanelItemView2) findViewById(R.id.item2);
        itemView3 = (PanelItemView2) findViewById(R.id.item3);
        itemView4 = (PanelItemView2) findViewById(R.id.item4);
        itemView6 = (PanelItemView2) findViewById(R.id.item6);
        itemView7 = (PanelItemView2) findViewById(R.id.item7);
        itemView8 = (PanelItemView2) findViewById(R.id.item8);
        itemView9 = (PanelItemView2) findViewById(R.id.item9);
    }

    public void setImgArray(List<NineListBean.NineBean> list) {
        // 0金币 1huawei手机  2提现券 3金币 4红包 5提现券 6金币 7phone

        if (list != null && list.size() >= 8) {
            itemView1.setStyle(list.get(0).getType(), list.get(0).getTitle());
            itemView2.setStyle(list.get(1).getType(), list.get(1).getTitle());
            itemView3.setStyle(list.get(2).getType(), list.get(2).getTitle());
            itemView6.setStyle(list.get(3).getType(), list.get(3).getTitle());
            itemView9.setStyle(list.get(4).getType(), list.get(4).getTitle());
            itemView8.setStyle(list.get(5).getType(), list.get(5).getTitle());
            itemView7.setStyle(list.get(6).getType(), list.get(6).getTitle());
            itemView4.setStyle(list.get(7).getType(), list.get(7).getTitle());

            itemViewArr[0] = itemView4;
            itemViewArr[1] = itemView1;
            itemViewArr[2] = itemView2;
            itemViewArr[3] = itemView3;
            itemViewArr[4] = itemView6;
            itemViewArr[5] = itemView9;
            itemViewArr[6] = itemView8;
            itemViewArr[7] = itemView7;
        }

    }

    private void stopMarquee() {
        isMarqueeRunning = false;
        isGameRunning = false;
        isTryToStop = false;
    }

    private void startMarquee() {
        isMarqueeRunning = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isMarqueeRunning) {
                    try {
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    post(new Runnable() {
                        @Override
                        public void run() {
                            if (bg_1 != null && bg_2 != null) {
                                if (VISIBLE == bg_1.getVisibility()) {
                                    bg_1.setVisibility(GONE);
                                    bg_2.setVisibility(VISIBLE);
                                } else {
                                    bg_1.setVisibility(VISIBLE);
                                    bg_2.setVisibility(GONE);
                                }
                            }
                        }
                    });
                }
            }
        }).start();
    }

    private long getInterruptTime() {
        currentTotal++;
        if (isTryToStop) {
            currentSpeed += 10;
            if (currentSpeed > DEFAULT_SPEED) {
                currentSpeed = DEFAULT_SPEED;
            }
        } else {
            if (currentTotal / itemViewArr.length > 0) {
                currentSpeed -= 10;
            }
            if (currentSpeed < MIN_SPEED) {
                currentSpeed = MIN_SPEED;
            }
        }
        return currentSpeed;
    }

    public boolean isGameRunning() {
        return isGameRunning;
    }

    public void startGame() {
        isGameRunning = true;
        isTryToStop = false;
        currentSpeed = DEFAULT_SPEED;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isGameRunning) {
                    try {
                        Thread.sleep(getInterruptTime());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    post(new Runnable() {
                        @Override
                        public void run() {
                            int preIndex = currentIndex;
                            currentIndex++;
                            if (currentIndex >= itemViewArr.length) {
                                currentIndex = 0;
                            }

                            itemViewArr[preIndex].setFocus(false);
                            itemViewArr[currentIndex].setFocus(true);

                            if (isTryToStop && currentSpeed == DEFAULT_SPEED && stayIndex == currentIndex) {
                                isGameRunning = false;
                            }
                        }
                    });
                }
            }
        }).start();
    }

    public void tryToStop(int position) {
        stayIndex = position;
        isTryToStop = true;
    }

}
