package com.gh.gamecenter;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
import com.gh.base.AppController;
import com.gh.base.BaseActivity;
import com.gh.common.util.DownloadItemUtils;
import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntity;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.adapter.SubjectAdapter;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPackage;

public class SubjectActivity extends BaseActivity {

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

	private RecyclerView subject_list;
	private SubjectAdapter adapter;
	private ProgressBarCircularIndeterminate subject_pb_loading;
	private LinearLayout reuse_no_connection;
	private LinearLayoutManager layoutManager;

	private String id;
	private String name;
	private String entrance;

	private int page = 1;

	private boolean order;
	private boolean isEverpause = false;

	// 黄壮华 添加观察者 修改2015/8/15
	private DataWatcher dataWatcher = new DataWatcher() {
		@Override
		public void onDataChanged(DownloadEntity downloadEntity) {
			Integer location = adapter.getLocationMap().get(downloadEntity.getName());
			if (location != null) {
				GameEntity detailedEntity = adapter.getList().get(location);
				if (detailedEntity != null) {
					DownloadItemUtils.processDate(SubjectActivity.this, detailedEntity, downloadEntity,
							adapter, location);
				}
			}
		}
	};

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

		entrance = getIntent().getStringExtra("entrance");

		id = getIntent().getStringExtra("id");
		name = getIntent().getStringExtra("name");
		order = getIntent().getBooleanExtra("order",false);

		View contentView = View.inflate(this, R.layout.activity_subject, null);
		init(contentView, name);

		reuse_no_connection.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				subject_pb_loading.setVisibility(View.VISIBLE);
				subject_list.setVisibility(View.VISIBLE);
				reuse_no_connection.setVisibility(View.GONE);
				adapter = new SubjectAdapter(SubjectActivity.this, id, name, order, entrance);
				subject_list.setAdapter(adapter);
			}
		});

		layoutManager = new LinearLayoutManager(this);

		subject_list.setHasFixedSize(true);
		subject_list.setLayoutManager(layoutManager);
		adapter = new SubjectAdapter(this, id, name, order, entrance);
		subject_list.setAdapter(adapter);

		subject_list.setOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				if (!isDestroy && adapter.isLoaded() && newState == RecyclerView.SCROLL_STATE_IDLE
						&& adapter.getItemCount() == layoutManager.findLastVisibleItemPosition() + 1){
					page++;
					adapter.initList(page);
				}
			}
		});

	}

	@Override
	public void loadError() {
		if (subject_pb_loading != null && subject_pb_loading.getVisibility() == View.VISIBLE) {
			subject_pb_loading.setVisibility(View.GONE);
		}
		toast("加载失败，请检查网络状态");
		subject_list.setVisibility(View.GONE);
		reuse_no_connection.setVisibility(View.VISIBLE);
	}

	@Override
	public void loadDone() {
		if (subject_pb_loading != null && subject_pb_loading.getVisibility() == View.VISIBLE) {
			subject_pb_loading.setVisibility(View.GONE);
		}
	}

	public void onEventMainThread(EBPackage busFour) {
		String name = adapter.getNameMap().get(busFour.getPackageName());
		if (name != null) {
			int location = adapter.getLocationMap().get(name);
			if ("安装".equals(busFour.getType())) {
				GameEntity entity = adapter.getList().get(location);
				for (ApkEntity apkEntity : entity.getApk()) {
					if (apkEntity.getPackageName().equals(
							busFour.getPackageName())) {
						entity.getEntryMap().remove(apkEntity.getPlatform());
						adapter.notifyItemChanged(location);
						break;
					}
				}
			} else if ("卸载".equals(busFour.getType())) {
				adapter.notifyItemChanged(location);
			}
		}
	}

	//连接上网络事件
	public void onEventMainThread(EBNetworkState busNetworkState) {
		if (busNetworkState.isNetworkConnected()) {
			if (reuse_no_connection.getVisibility() == View.VISIBLE) {
				subject_list.setVisibility(View.VISIBLE);
				subject_pb_loading.setVisibility(View.VISIBLE);
				reuse_no_connection.setVisibility(View.GONE);
				adapter = new SubjectAdapter(SubjectActivity.this, id, name, order, entrance);
				subject_list.setAdapter(adapter);
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (isEverpause) {
			for (GameEntity entity : adapter.getList()) {
				entity.setEntryMap(DownloadManager.getInstance(this).getEntryMap(entity.getName()));
			}
		}
		isEverpause = false;
		DownloadManager.getInstance(getApplicationContext()).addObserver(dataWatcher);
	}

	@Override
	public void onPause() {
		super.onPause();
		isEverpause = true;
		DownloadManager.getInstance(getApplicationContext()).removeObserver(dataWatcher);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		AppController.canclePendingRequests(TAG);
		subject_list = null;
		adapter = null;
		subject_pb_loading = null;
		reuse_no_connection = null;
		dataWatcher = null;
	}
}
