package com.qiyou.mb.android.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qiyou.mb.android.R;
import com.qiyou.mb.android.ui.MainActivity;
import com.qiyou.mb.android.utils.HackyViewPager;

public class ImagePager_fragment extends QiyouFragment {
//	public static final String TAG ="com.qiyou.ImagePager_fragment";
//	public static final String dTAG ="com.qiyou";
	
	@Override
	public void setFTag(){
		TAG="com.qiyou.ImagePager_fragment";
	}
	public static String getFTag(){
		return "com.qiyou.ImagePager_fragment";
	}
	@Override
	public String getCurrentTag(){
		return "com.qiyou.ImagePager_fragment";
	}
	private static final String STATE_POSITION = "STATE_POSITION";
	public static final String EXTRA_IMAGE_INDEX = "image_index";
	public static final String EXTRA_IMAGE_URLS = "image_urls";

	private HackyViewPager mPager;
	private int pagerPosition;
	private TextView indicator;
	private View vContainer;
	protected MainActivity activity;
	String[] imgUrls;
	public static ImagePager_fragment newInstance(int posision,String[] urls)
	{
	 
		ImagePager_fragment f = new ImagePager_fragment();
	 
		Bundle args = new Bundle(); 
	    args.putInt(EXTRA_IMAGE_INDEX, posision);
	    args.putStringArray(EXTRA_IMAGE_URLS, urls);
	    f.setArguments(args);
	    return f;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		   
		activity = (MainActivity)getActivity();  
//		ImageLoader.getInstance().destroy();
//		activity.app.initialImageLoader();
		
	   
		   Bundle bd =this.getArguments();
		pagerPosition = bd.getInt(EXTRA_IMAGE_INDEX, 0);
		imgUrls =bd.getStringArray(EXTRA_IMAGE_URLS); 
		initialView(  savedInstanceState); 
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		    vContainer = inflater.inflate(R.layout.image_detail_pager, container, false);	 
			return vContainer;
	}
	void initialView(Bundle savedInstanceState){
		mPager = (HackyViewPager) vContainer.findViewById(R.id.pager);
		ImagePagerAdapter mAdapter = new ImagePagerAdapter(
//				activity.getSupportFragmentManager(),
				getChildFragmentManager(), //ViewPager is layouted inside a fragment (not an activty)
				imgUrls);
		mPager.setAdapter(mAdapter);
		indicator = (TextView)vContainer. findViewById(R.id.indicator);

		CharSequence text = getString(R.string.viewpager_indicator, 1, mPager
				.getAdapter().getCount());
		indicator.setText(text);
		// 鏇存柊涓嬫爣
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int arg0) {
				CharSequence text = getString(R.string.viewpager_indicator,
						arg0 + 1, mPager.getAdapter().getCount());
				indicator.setText(text);
			}

		});
		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}

		mPager.setCurrentItem(pagerPosition);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mPager.getCurrentItem());
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter {

		public String[] fileList;

		public ImagePagerAdapter(FragmentManager fm, String[] fileList) {
			super(fm);
			this.fileList = fileList;
		}

		@Override
		public int getCount() {
			return fileList == null ? 0 : fileList.length;
		}

		@Override
		public Fragment getItem(int position) {
			String url = fileList[position];
			return ImageDetailFragment.newInstance(url);
		}

	}
	
	
	@Override
	public void onResume() {
		super.onResume();			 
//	   	activity.app.currentActiveFragment =TAG;     
	}
	 @Override
	public boolean isShowBottomTabs() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void setTabOnFiling() {
		// TODO Auto-generated method stub
		
	} 
	
}