package com.lee.main.view.pager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.lee.base.activity.BaseFragmentActivity;
import com.lee.main.R;
import com.lee.main.fragment.TestFragment;
import com.lee.main.view.pager.slide.NoSlideViewPager;

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

/**
 * No Slide ViewPager Activity
 * </p>
 * Created by LiYang on 2017/1/15.
 */

public class NoSlideViewPagerActivity extends BaseFragmentActivity {

    private NoSlideViewPager noSlideViewPager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_slide_view_pager);

        noSlideViewPager = (NoSlideViewPager) findViewById(R.id.no_slide_view_pager);
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);

        radioGroup.setOnCheckedChangeListener(checkedChangeListener);
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), getData());
        noSlideViewPager.setAdapter(fragmentPagerAdapter);
    }

    private List<Fragment> getData() {
        List<Fragment> data = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            TestFragment fragment = TestFragment.newInstance("Fragment" + (i + 1));
            data.add(fragment);
        }
        return data;
    }

    private RadioGroup.OnCheckedChangeListener checkedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.btn_radio_group_1:
                    noSlideViewPager.setCurrentItem(0,false);
                    break;

                case R.id.btn_radio_group_2:
                    noSlideViewPager.setCurrentItem(1,false);
                    break;

                case R.id.btn_radio_group_3:
                    noSlideViewPager.setCurrentItem(2,false);
                    break;

                case R.id.btn_radio_group_4:
                    noSlideViewPager.setCurrentItem(3,false);
                    break;
            }
        }
    };

    public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {

        List<Fragment> mFragmentList = null;

        public FragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> list) {
            super(fragmentManager);
            mFragmentList = list;
        }

        @Override
        public Fragment getItem(int position) {
            if(null != mFragmentList){
                return mFragmentList.get(position);
            }
            return null;
        }

        @Override
        public int getCount() {
            if(null != mFragmentList){
                return mFragmentList.size();
            }
            return 0;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            //super.setPrimaryItem(container, position, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
            return fragment;
        }
    }

}
