package com.android.chmo.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.android.chmo.R;
import com.android.chmo.app.SpManager;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.ui.view.PageIndexView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class GuideActivity extends BaseActivity {
    private final int imageIds[]= new int[]{R.mipmap.guide1, R.mipmap.guide2,R.mipmap.guide3};

    @BindView(R.id.pager)
    ViewPager viewPager;
    @BindView(R.id.index)
    PageIndexView indexView;
    @BindView(R.id.launch)
    Button launchBtn;

    @Override
    protected int getContentResId() {
        return R.layout.activity_guide;
    }

    @Override
    public void initView() {
        indexView.setDefDrawable(R.drawable.index_point_uncheck);
        indexView.setSelectedIndexDrawable(R.drawable.index_point_checked);

        if (imageIds.length > 1) {
            indexView.setCount(imageIds.length);
            indexView.setCurIndex(0);
            indexView.setVisibility(View.VISIBLE);
        } else {
            indexView.setVisibility(View.GONE);
        }

        viewPager.setAdapter(new ImagePagerAdapter(getSupportFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indexView.setCurIndex(position);
                if(position == imageIds.length-1) {
                    launchBtn.setVisibility(View.VISIBLE);
                }else{
                    launchBtn.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    @OnClick(R.id.launch)
    public void launch() {
        SpManager.getInstance().setBoolean("appStarted", true);
        openPage(LoginActivity.class);
        finish();
    }



    public static class ImageFragment extends Fragment {
        private ImageView imageIV;
        private int imageId;

        public ImageFragment() {
            super();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.page_guide, container, false);
            imageIV = (ImageView) view.findViewById(R.id.image);

            imageId = getArguments().getInt("imageId");
            imageIV.setImageResource(imageId);

            return view;
        }
    }

    private class ImagePagerAdapter extends FragmentPagerAdapter {
        public ImagePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = Fragment.instantiate(getApplicationContext(),
                    ImageFragment.class.getName());
            Bundle args = new Bundle();
            args.putInt("imageId", imageIds[position]);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return imageIds.length;
        }
    }
}
