package com.gh.gamecenter.download;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.base.AppController;
import com.gh.gamecenter.MainActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBDownloadChanged;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBMiPush;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.manager.PackageManager;

import de.greenrobot.event.EventBus;

/**
 * 游戏更新管理fragment
 * 
 * @author 黄壮华
 * 
 */
public class GameUpdateFragment extends Fragment {

	public static final String TAG = GameUpdateFragment.class.getSimpleName();

	private View view;
	private RecyclerView gameupdate_rv_show;
	private GameUpdateAdapter adapter;
	private LinearLayout gameupdate_ll_loading;
	private LinearLayout reuse_nodata_skip;

	private String packageName;
	private boolean isUpdate;

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("packageName", packageName);
		outState.putBoolean("isUpdate", isUpdate);
	}

	public void setParams(String str, boolean flag) {
		packageName = str;
		isUpdate = flag;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (savedInstanceState != null) {
			if (savedInstanceState.containsKey("packageName")) {
				packageName = savedInstanceState.getString("packageName");
			}
			if (savedInstanceState.containsKey("isUpdate")) {
				isUpdate = savedInstanceState.getBoolean("isUpdate");
			}
		}

		view = View.inflate(getActivity(), R.layout.gameupdate, null);

		gameupdate_ll_loading = (LinearLayout) view.findViewById(R.id.gameupdate_ll_loading);

		reuse_nodata_skip = (LinearLayout) view.findViewById(R.id.reuse_nodata_skip);
		reuse_nodata_skip.setVisibility(View.GONE);
		TextView reuse_nodata_skip_tv_hint = (TextView) view.findViewById(R.id.reuse_nodata_skip_tv_hint);
		reuse_nodata_skip_tv_hint.setText("暂无更新");
		TextView reuse_nodata_skip_tv_btn = (TextView) view.findViewById(R.id.reuse_nodata_skip_tv_btn);
		reuse_nodata_skip_tv_btn.setText("去首页看看");
		reuse_nodata_skip_tv_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				getActivity().startActivity(intent);
				new Thread(){
					@Override
					public void run() {
						try {
							sleep(300);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						EventBus.getDefault().post(new EBSkip("GameFragment", 0));
					}
				}.start();
			}
		});

		gameupdate_rv_show = (RecyclerView) view.findViewById(R.id.gameupdate_rv_show);
		gameupdate_rv_show.setHasFixedSize(true);
		gameupdate_rv_show.setLayoutManager(new LinearLayoutManager(getActivity()));
		adapter = new GameUpdateAdapter(getActivity(), gameupdate_ll_loading,
				reuse_nodata_skip, packageName, isUpdate);
		gameupdate_rv_show.setAdapter(adapter);

		EventBus.getDefault().register(this);
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if (container != null) {
			container.removeView(view);
		}
		return view;
	}

	public void onEventMainThread(EBDownloadStatus status) {
		if ("delete".equals(status.getStatus())) {
			String packageName = adapter.getPackageNameMap().get(status.getUrl());
			if (packageName != null) {
				Integer location = adapter.getLocationMap().get(packageName);
				adapter.getEntryMap().remove(location);
				gameupdate_rv_show.getAdapter().notifyItemChanged(location + 1);
				gameupdate_rv_show.getAdapter().notifyItemChanged(0);
			}
		}
	}

	public void onEventMainThread(EBPackage busFour) {
		if ("安装".equals(busFour.getType())) {
			Integer location = adapter.getLocationMap().get(busFour.getPackageName());
			if (location != null) {
				adapter.getSuccessMap().put(location, true);
				EventBus.getDefault().post(
						new EBDownloadChanged("update", View.VISIBLE, -1));
				gameupdate_rv_show.getAdapter().notifyItemChanged(location + 1);
				gameupdate_rv_show.getAdapter().notifyItemChanged(0);
				PackageManager.removeUpdateList(busFour.getPackageName());
			}
		} else if ("卸载".equals(busFour.getType())) {
			Integer location = adapter.getLocationMap().get(busFour.getPackageName());
			if (location != null && adapter.getUpdateList().size() != 0) {
				if (adapter.getUpdateList().get(location).isPluggable()) {
					EventBus.getDefault().post(
							new EBDownloadChanged("update", View.VISIBLE, -1));
					if (adapter.getUpdateList().size() == 1) {
						adapter.getUpdateList().remove(location.intValue());
						adapter.notifyItemRangeRemoved(0, 2);
						if (reuse_nodata_skip.getVisibility() == View.GONE) {
							reuse_nodata_skip.setVisibility(View.VISIBLE);
						}
					} else {
						adapter.getUpdateList().remove(location.intValue());
						adapter.notifyItemRemoved(location + 1);
						adapter.notifyItemChanged(0);
					}
				}
			}
		}
	}

	public void onEventMainThread(EBMiPush miPush) {
		if ("mipush_plugin".equals(miPush.getFrom())) {
			adapter.getUpdateListFromServer();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
		AppController.canclePendingRequests(TAG);
		view = null;
		gameupdate_rv_show = null;
		gameupdate_ll_loading = null;
		reuse_nodata_skip = null;
		packageName = null;
	}
}
