package com.wasu.cs.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;

import com.wasu.cs.adapter.BlockPageAdapter;
import com.wasu.cs.widget.FixedSpeedScroller;
import com.wasu.module.log.WLog;

import java.lang.reflect.Field;

import cn.com.wasu.main.R;

/**
 * 4.0二级列表的Container内容部分，使用GridView展示资产item
 * Created by LiuYang on 6/23/15.
 */
public class FragmentBlockContainer extends FragmentChannelBase {

    private ViewPager mViewPager;
    private BlockPageAdapter mPageAdapter;
    private FragmentPageBase mPrevFragment;
    private static final String TAG = FragmentBlockContainer.class.getSimpleName();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_block_container,container ,false);
        mViewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(3);
        setScrollerTime(500);
        return rootView;
    }

    @Override
    protected boolean onBackPressed() {
        int currentPage = mViewPager.getCurrentItem();
        FragmentPageBase fragment = (FragmentPageBase) mPageAdapter.getItem(currentPage);
        if(fragment == null){
        	WLog.e(TAG, "OOM导致被强制GC回收Fragment");
        }
        fragment.onBackPressed();
        return false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initPageView();
    }

    void initPageView() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (mPrevFragment != null) {
                    mPrevFragment.onUnSelected();
                }

                int currentPage = mViewPager.getCurrentItem();
                FragmentPageBase fragment = (FragmentPageBase) mPageAdapter.getItem(currentPage);
                if (fragment != null) {
                    fragment.onSelected();
                    mPrevFragment = fragment;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void setAdapter(BlockPageAdapter adapter) {
        this.mPageAdapter = adapter;
        mViewPager.setAdapter(mPageAdapter);
    }

    public ViewPager getViewPager() {
        return mViewPager;
    }

    public View focusSearch(View focused, int direction) {
    	/**根据tabItem中的cat数据来创建对应的fragment**/
        FragmentPageBase fragment = (FragmentPageBase) mPageAdapter.getItem(mViewPager.getCurrentItem());
        if(null != fragment){
        	return fragment.focusSearch(focused,direction);
        }else{
        	return null;
        }
    }
    
	private FixedSpeedScroller scroller = null;

	public void setScrollerTime(int scrollerTime) {
		try {
			if (scroller != null) {
				scroller.setTime(scrollerTime);
			} else {
				Field mScroller;
				mScroller = ViewPager.class.getDeclaredField("mScroller");
				mScroller.setAccessible(true);
				scroller = new FixedSpeedScroller(mViewPager.getContext(),
						new DecelerateInterpolator());
				scroller.setTime(scrollerTime);
				mScroller.set(mViewPager, scroller);
			}
		} catch (Exception e) {
		}
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		cleanResource();
	}
	
	public void cleanResource(){
		if(mPageAdapter != null){
			mPageAdapter.clearData();
			mPageAdapter = null;
		}
		if(mViewPager != null){
			mViewPager.removeAllViews();
			mViewPager = null;
		}
		if(mPrevFragment != null){
			mPrevFragment = null;
		}
		if(scroller != null){
			scroller = null;
		}
	}

    /**
     * 跳转下一页
     */
    public void requestNextView() {
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
    }

    /**
     * 跳转上一页
     */
    public void requestPreView() {
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
    }
}
