package com.etime.green;

import com.etime.adapter.ContentPagerAdapter;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class LeftSlidingMenuFragment extends Fragment implements OnClickListener{
	MainActivity appContext;
	MainActivity activity;
	ViewPager vpager;
	LinearLayout  relayout = null;
	LinearLayout paiLayout = null;
	private FrameLayout mFrameLayout = null;
	
	private ImageButton nav_bt_hotboard;
	private FrameLayout  frame32;
	private ImageButton nav_bt_contest;
	private FrameLayout  frame31;
	private ImageButton nav_bt_leaderboard;
	private FrameLayout frame12;
	private ViewPager pVpager;
	private ImageButton nav_bt_setting;
	
	private Button p_leftbtn;
	private Button p_midbtn;
	private Button p_rightbtn;
     @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
//    	appContext = (MainActivity)getActivity().getApplicationContext();
    }
     
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.left_nav_menu_improve, container,
				false);
		activity = (MainActivity) getActivity();
		nav_bt_hotboard = (ImageButton) view.findViewById(R.id.nav_bt_hotboard);
		nav_bt_hotboard.setOnClickListener(this);
		frame32 = (FrameLayout) view.findViewById(R.id.frame_32);
		
		nav_bt_contest = (ImageButton) view.findViewById(R.id.nav_bt_contest);
		nav_bt_contest.setOnClickListener(this);
		frame31 = (FrameLayout) view.findViewById(R.id.frame_31);
		
		nav_bt_leaderboard = (ImageButton) view.findViewById(R.id.nav_bt_leaderboard);
		nav_bt_leaderboard.setOnClickListener(this);
		frame12 = (FrameLayout) view.findViewById(R.id.frame_12);
		pVpager = (ViewPager) activity.findViewById(R.id.pvPager);
		
		nav_bt_setting = (ImageButton) view.findViewById(R.id.nav_bt_setting);
		nav_bt_setting.setOnClickListener(this);
		
		relayout = (LinearLayout) activity.findViewById(R.id.mainviewpager);
		paiLayout =  (LinearLayout) activity.findViewById(R.id.paihangVp);
		mFrameLayout = (FrameLayout) activity.findViewById(R.id.content_frame);
		p_leftbtn = (Button) activity.findViewById(R.id.plfet_button);
		p_midbtn = (Button) activity.findViewById(R.id.pmid_button);
		p_rightbtn = (Button) activity.findViewById(R.id.pright_button);
		return view;
    }

	@Override
	public void onClick(View v) {
		Fragment newContent = null;
		switch (v.getId()) {
		case R.id.nav_bt_hotboard:
			relayout.setVisibility(View.GONE);
			paiLayout.setVisibility(View.GONE);
            mFrameLayout.setVisibility(View.VISIBLE);
            newContent = new Fragment_Personal();
            frame31.setSelected(false);
            frame32.setSelected(true);
            frame12.setSelected(false);
			break;
		case R.id.nav_bt_contest:
			activity.getSlidingMenu().toggle();
			relayout.setVisibility(View.VISIBLE);
            mFrameLayout.setVisibility(View.GONE);
            paiLayout.setVisibility(View.GONE);
			frame31.setSelected(true);
			frame32.setSelected(false);
			frame12.setSelected(false);
			break;
		case R.id.nav_bt_leaderboard:
			activity.getSlidingMenu().toggle();
			relayout.setVisibility(View.GONE);
            mFrameLayout.setVisibility(View.GONE);
            paiLayout.setVisibility(View.VISIBLE);
            pVpager.setAdapter(new ContentPagerAdapter(activity.getSupportFragmentManager()));
            pVpager.setOnPageChangeListener(new OnPageChangeListener() {

    			@Override
    			public void onPageSelected(int position) {
    				setCurrentPage(position);
    			}

    			@Override
    			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    				// ignore
    			}

    			@Override
    			public void onPageScrollStateChanged(int state) {
    				// ignore
    			}
    		});
            frame31.setSelected(false);
            frame32.setSelected(false);
            frame12.setSelected(true);
			break;
		case R.id.nav_bt_setting:
			relayout.setVisibility(View.GONE);
			paiLayout.setVisibility(View.GONE);
            mFrameLayout.setVisibility(View.VISIBLE);
            newContent = new Fragment_Setting();
			break;
		default:
			break;
		}
		if (newContent != null) {
			switchFragment(newContent);
		}
	}
	

	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;	
		activity.switchContent(fragment);
	}

	protected void setCurrentPage(int position) {
		// TODO Auto-generated method stub
		if(position==0){
			p_leftbtn.setSelected(true);
			p_midbtn.setSelected(false);
			p_rightbtn.setSelected(false);
			activity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		}else if(position==1){
			p_leftbtn.setSelected(false);
			p_midbtn.setSelected(true);
			p_rightbtn.setSelected(false);
			activity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		}else if(position==2){
			p_leftbtn.setSelected(false);
			p_midbtn.setSelected(false);
			p_rightbtn.setSelected(true);
			activity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		}
	}
	
}
