package com.gh.gamecenter;

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.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.base.BaseFragmentActivity;
import com.gh.common.util.DisplayUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.download.GameDownLoadFragment;
import com.gh.gamecenter.download.GameUpdateFragment;
import com.gh.gamecenter.eventbus.EBDownloadChanged;
import com.gh.gamecenter.eventbus.EBMiPush;
import com.gh.gamecenter.eventbus.EBPerformClick;
import com.gh.gamecenter.eventbus.EBUISwitch;
import com.gh.gamecenter.manager.PackageManager;

import de.greenrobot.event.EventBus;

/**
 * 下载更新管理页面
 * 
 * @author 黄壮华
 * 
 */
public class DownloadManagerActivity extends BaseFragmentActivity implements
		OnClickListener, OnPageChangeListener {

	private ViewPager downloadmanager_viewPager;
	private TextView downloadmanager_tv_download;
	private TextView downloadmanager_tv_update;
	private TextView downloadmanager_download_number;
	private TextView downloadmanager_update_number;
	private LinearLayout downloadmanager_ll_download;
	private LinearLayout downloadmanager_ll_update;
	private View downloadmanager_slide_line;
	private LinearLayout.LayoutParams lparams;

	private int width;

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("CurrentItem", downloadmanager_viewPager.getCurrentItem());
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		View contentView = View.inflate(this, R.layout.activity_downloadmanager, null);
		init(contentView, "下载管理");

		downloadmanager_ll_download.setOnClickListener(this);
		downloadmanager_ll_update.setOnClickListener(this);

		int updateSize = PackageManager.getUpdateList().size();
		if (updateSize != 0) {
			downloadmanager_update_number.setVisibility(View.VISIBLE);
			downloadmanager_update_number.setText(String.valueOf(updateSize));
		} else {
			downloadmanager_update_number.setVisibility(View.GONE);
		}

		int downloadSize = DownloadManager.getInstance(
				getApplicationContext()).getAll().size();
		if (downloadSize != 0) {
			downloadmanager_download_number.setVisibility(View.VISIBLE);
			downloadmanager_download_number.setText(String.valueOf(downloadSize));
		} else {
			downloadmanager_download_number.setVisibility(View.GONE);
		}

		int currentItem = getIntent().getIntExtra("currentItem", -1);
		if (currentItem == -1) {
			if (updateSize != 0 && downloadSize == 0) {
				currentItem = 1;
			} else {
				currentItem = 0;
			}
		}

		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		width = outMetrics.widthPixels / 2;
		lparams = new LinearLayout.LayoutParams(width / 2, DisplayUtils.dip2px(
				getApplicationContext(), 2));
		lparams.leftMargin = outMetrics.widthPixels / 8;
		if (currentItem != 0) {
			downloadmanager_ll_download.setClickable(true);
			downloadmanager_ll_update.setClickable(false);
			lparams.leftMargin = width + width / 4;
		} else {
			downloadmanager_ll_download.setClickable(false);
			downloadmanager_ll_update.setClickable(true);
		}
		downloadmanager_slide_line.setLayoutParams(lparams);

		downloadmanager_viewPager.setAdapter(new FragmentAdapter(
				getSupportFragmentManager()));
		downloadmanager_viewPager.addOnPageChangeListener(this);

		if (savedInstanceState != null) {
			lparams.leftMargin = width * savedInstanceState.getInt("CurrentItem") + width / 4;
			downloadmanager_slide_line.setLayoutParams(lparams);
		} else {
			downloadmanager_viewPager.setCurrentItem(currentItem);
		}
	}

	private class FragmentAdapter extends FragmentPagerAdapter {

		public FragmentAdapter(FragmentManager fm) {
			super(fm);
			
		}

		@Override
		public Fragment getItem(int position) {
			if (position == 0) {
				GameDownLoadFragment fragment = new GameDownLoadFragment();
				fragment.setPath(getIntent().getStringExtra("path"));
				return fragment;
			} else {
				GameUpdateFragment fragment = new GameUpdateFragment();
				fragment.setParams(getIntent().getStringExtra(
						"packageName"), getIntent().getBooleanExtra(
						"isPushIntent", false));
				return fragment;
			}
		}

		@Override
		public int getCount() {
			
			return 2;
		}

	}

	@Override
	public void onPageSelected(int position) {
		
		if (position == 0) {
			EventBus.getDefault().post(
					new EBUISwitch("DownloadManagerActivity", 0));
			downloadmanager_ll_download.setClickable(false);
			downloadmanager_ll_update.setClickable(true);
			downloadmanager_tv_download.setTextColor(getResources().getColor(R.color.theme));
			downloadmanager_tv_update.setTextColor(getResources().getColor(R.color.title));
		} else {
			EventBus.getDefault().post(
					new EBUISwitch("DownloadManagerActivity", 1));
			downloadmanager_ll_download.setClickable(true);
			downloadmanager_ll_update.setClickable(false);
			downloadmanager_tv_download.setTextColor(getResources().getColor(R.color.title));
			downloadmanager_tv_update.setTextColor(getResources().getColor(R.color.theme));
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		
		if (positionOffset != 0) {
			lparams.leftMargin = (int) (width * positionOffset) + width / 4;
			downloadmanager_slide_line.setLayoutParams(lparams);
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {
		

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.actionbar_rl_back:
			finish();
			break;
		case R.id.downloadmanager_ll_download:
			downloadmanager_viewPager.setCurrentItem(0);
			break;
		case R.id.downloadmanager_ll_update:
			downloadmanager_viewPager.setCurrentItem(1);
			break;
		default:
			break;
		}
	}

	public void onEventMainThread(EBMiPush mipush){
		if ("plugin_install".equals(mipush.getFrom())) {
			downloadmanager_ll_download.performClick();
		}
	}
	
	public void onEventMainThread(EBDownloadChanged changed) {
		if ("download".equals(changed.getType())) {
			if (changed.getVisibility() == View.VISIBLE){
				downloadmanager_download_number.setVisibility(View.VISIBLE);
			} else if (changed.getVisibility() == View.GONE) {
				downloadmanager_download_number.setVisibility(View.GONE);
			} else if (changed.getVisibility() == View.INVISIBLE) {
				downloadmanager_download_number.setVisibility(View.INVISIBLE);
			}
			downloadmanager_download_number.setText(String.valueOf(changed.getSize()));
		} else if ("update".equals(changed.getType())) {
			if (changed.getSize() == -1) {
				int number = Integer.valueOf(downloadmanager_update_number
						.getText().toString());
				if (number == 1) {
					downloadmanager_update_number.setVisibility(View.GONE);
				} else {
					downloadmanager_update_number.setText(String.valueOf(number - 1));
				}
			} else {
				if (changed.getSize() != 0) {
					if (changed.getVisibility() == View.VISIBLE){
						downloadmanager_update_number.setVisibility(View.VISIBLE);
					} else if (changed.getVisibility() == View.GONE) {
						downloadmanager_update_number.setVisibility(View.GONE);
					} else if (changed.getVisibility() == View.INVISIBLE) {
						downloadmanager_update_number.setVisibility(View.INVISIBLE);
					}
					downloadmanager_update_number.setText(String.valueOf(changed.getSize()));
				} else {
					downloadmanager_update_number.setVisibility(View.GONE);
				}
			}
		}
	}
	
	public void onEventMainThread(EBPerformClick click) {
		if (click.getFrom().equals("update")) {
			downloadmanager_ll_download.performClick();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		downloadmanager_viewPager = null;
		downloadmanager_tv_download = null;
		downloadmanager_tv_update = null;
		downloadmanager_download_number = null;
		downloadmanager_update_number = null;
		downloadmanager_ll_download = null;
		downloadmanager_ll_update = null;
		downloadmanager_slide_line = null;
		lparams = null;
	}
}