package com.sheep.gamegroup.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.sheep.gamegroup.view.adapter.ViewPagerFragmentAdapter;
import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.view.fragment.FgtImage;
import com.sheep.jiuyan.samllsheep.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;

/**
 * Created by realicing on 2018/6/1.
 * realicing@sina.com
 */
public class ActGuide extends BaseActivity {
    private int[] ids = {R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
    private List<View> viewList = ListUtil.emptyList();
    @Override
    protected int getLayoutId() {
        return R.layout.act_guide;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        List<Fragment> fragmentList = ListUtil.emptyList();
        for (int i = 0; i < ids.length; i++) {
            fragmentList.add(FgtImage.newInstance(ids[i]));
            View view = View.inflate(this, R.layout.dian, null);
            int radius = getResources().getDimensionPixelSize(R.dimen.content_padding_10);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(radius,radius);
            if(i != 0)
                layoutParams.leftMargin =  radius;
            guide_tab.addView(view, layoutParams);
            viewList.add(view);
            viewList.get(i).setEnabled(i == 0);
        }
        ViewPagerFragmentAdapter adapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList);
        guide_vp.setAdapter(adapter);
        guide_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int index) {
                for (int i = 0; i < viewList.size(); i++) {
                    viewList.get(i).setEnabled(i == index);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @BindView(R.id.guide_vp)
    ViewPager guide_vp;
    @BindView(R.id.guide_tab)
    LinearLayout guide_tab;


    @Subscribe
    public void onEventMainThread(BigEvent event) {
        if (event.getEventTypes() == EventTypes.GUIDE_NEXT) {
            int index = guide_vp.getCurrentItem() + 1;
            if(index == ids.length)
                toSkipNext(null);
            else
                guide_vp.setCurrentItem(index);
        }
    }
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    /**
     * 点击跳过
     * @param view
     */
    public void toSkipNext(View view) {
        Jump2View.getInstance().gotoMain(this);
    }
}
