package com.gh.gamecenter;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gh.base.AppController;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.ConcernUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.Utils;
import com.gh.common.view.Concern_LinearLayout;
import com.gh.gamecenter.adapter.ConcernAdapter;
import com.gh.gamecenter.adapter.ConcernRecommendAdapter;
import com.gh.gamecenter.db.info.ConcernInfo;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.manager.ConcernManager;

import org.json.JSONArray;

import java.util.List;

public class ConcernActivity extends BaseActivity implements OnClickListener {

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

	private Concern_LinearLayout view;
	private RecyclerView concern_rv_show, concern_rv_recommend;
	private ConcernAdapter concernAdapter;
	private ConcernRecommendAdapter concernRecommendAdapter;
	private RelativeLayout concern_rl_title;
	private LinearLayout reuse_none_data;

	private ConcernManager concernManager;

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

		view = (Concern_LinearLayout) View.inflate(this, R.layout.activity_concern, null);
		init(view, "我的关注");

		concernManager = new ConcernManager(getApplicationContext());

		List<ConcernInfo> concernList = concernManager.getConcernGame();
		if (concernList == null || concernList.isEmpty()) {
			reuse_none_data.setVisibility(View.VISIBLE);
			concern_rv_show.setVisibility(View.GONE);
		}

		concern_rl_title.setOnClickListener(this);

		concern_rv_show.setHasFixedSize(true);
		concern_rv_show.setLayoutManager(new GridLayoutManager(this, 3));
		concernAdapter = new ConcernAdapter(this);
		concern_rv_show.setAdapter(concernAdapter);

		concern_rv_recommend.setHasFixedSize(true);
		concern_rv_recommend.setLayoutManager(new GridLayoutManager(this, 4));
		concernRecommendAdapter = new ConcernRecommendAdapter(this);
		concern_rv_recommend.setAdapter(concernRecommendAdapter);
	}

	@Override
	public void loadEmpty() {
		if (concernAdapter.getConcernList() == null
				|| concernAdapter.getConcernList().isEmpty()) {
			reuse_none_data.setVisibility(View.VISIBLE);
			concern_rv_show.setVisibility(View.GONE);
		}
		if (concernRecommendAdapter.getRecommendGameList() == null
				|| concernRecommendAdapter.getRecommendGameList().isEmpty()) {
			concern_rl_title.setVisibility(View.GONE);
			concern_rv_recommend.setVisibility(View.GONE);
		} else {
			if (concernRecommendAdapter.getRecommendGameList().size() < 4) {
				concern_rv_recommend.setLayoutManager(new GridLayoutManager(
						ConcernActivity.this, concernRecommendAdapter.getRecommendGameList().size()));
			}
		}
	}

	@Override
	public void loadDone() {
		concern_rl_title.setVisibility(View.VISIBLE);
		concern_rv_recommend.setVisibility(View.VISIBLE);
		int size = concernRecommendAdapter.getRecommendGameList().size();
		concern_rv_recommend.setLayoutManager(new GridLayoutManager(this, size > 4 ? 4 : size));
		concern_rv_recommend.getAdapter().notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		if (v == concern_rl_title) {
			if (concern_rv_recommend.getVisibility() == View.VISIBLE) {
				view.hideRecyclerView();
			} else {
				view.showRecyclerView();
			}
		}
	}

	// 关注事件
	public void onEventMainThread(EBConcernChanged changed) {
		Utils.log("changed = " + changed.getGameId());
		Utils.log("changed = " + changed.isConcern());
		if (changed.isConcern()) {
			for (GameEntity gameEntity : concernRecommendAdapter.getGameList()) {
				if (changed.getGameId().equals(gameEntity.getId())) {
					ConcernInfo concernInfo = new ConcernInfo();
					concernInfo.setGameName(gameEntity.getName());
					concernInfo.setConcern(true);
					concernInfo.setIcon(gameEntity.getIcon());
					concernInfo.setId(gameEntity.getId());
					concernAdapter.getConcernList().add(concernInfo);
					concernAdapter.getConcernGameList().add(gameEntity);
					if (reuse_none_data.getVisibility() == View.VISIBLE) {
						reuse_none_data.setVisibility(View.GONE);
						concern_rv_show.setVisibility(View.VISIBLE);
					}
					concernAdapter.notifyDataSetChanged();
				}
			}
			for (int i = 0, size = concernRecommendAdapter.getRecommendGameList().size(); i < size; i++) {
				if (changed.getGameId().equals(concernRecommendAdapter.getRecommendGameList().get(i).getId())) {
					concernRecommendAdapter.getRecommendGameList().remove(i);
					concernRecommendAdapter.notifyDataSetChanged();
					if (concernRecommendAdapter.getRecommendGameList().isEmpty()) {
						concern_rl_title.setVisibility(View.GONE);
						concern_rv_recommend.setVisibility(View.GONE);
					}
					if (concernRecommendAdapter.getRecommendGameList().size() < 4) {
						concern_rv_recommend.setLayoutManager(new GridLayoutManager(
								ConcernActivity.this, concernRecommendAdapter.getRecommendGameList().size()));
					} else {
						concern_rv_recommend.setLayoutManager(new GridLayoutManager(
								ConcernActivity.this, 4));
					}
					break;
				}
			}
		} else {
			concernRecommendAdapter.getRecommendGameList().clear();
			for (int i = 0, size = concernRecommendAdapter.getGameList().size(); i < size; i++) {
				if (!concernManager.isConcern(concernRecommendAdapter.getGameList().get(i).getId())) {
					concernRecommendAdapter.getRecommendGameList().add(concernRecommendAdapter.getGameList().get(i));
				}
			}
			concernRecommendAdapter.notifyDataSetChanged();
			if (concernRecommendAdapter.getRecommendGameList().isEmpty()) {
				concern_rl_title.setVisibility(View.GONE);
				concern_rv_recommend.setVisibility(View.GONE);
			} else {
				concern_rl_title.setVisibility(View.VISIBLE);
				concern_rv_recommend.setVisibility(View.VISIBLE);
			}
			if (concernRecommendAdapter.getRecommendGameList().size() < 4) {
				concern_rv_recommend.setLayoutManager(new GridLayoutManager(
						ConcernActivity.this, concernRecommendAdapter.getRecommendGameList().size()));
			} else {
				concern_rv_recommend.setLayoutManager(new GridLayoutManager(
						ConcernActivity.this, 4));
			}
		}
	}

    private boolean isPause = false;

    @Override
    protected void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Utils.log("isPause = " + isPause);
        if (isPause) {
            List<ConcernInfo> concernList = concernManager.getConcernGame();
            boolean isChanged = false;
            if (concernList == null) {
                if (concernAdapter.getConcernList().size() != 0){
                    isChanged = true;
                }
            } else {
                if (concernAdapter.getConcernList() == null
		                || concernAdapter.getConcernList().isEmpty()
                        || concernList.size() > concernAdapter.getConcernList().size()
                        || concernList.size() < concernAdapter.getConcernList().size()) {
                    isChanged = true;
                }
            }
            Utils.log("isChanged = " + isChanged);
            if (isChanged) {
	            concernAdapter = new ConcernAdapter(this);
	            concern_rv_show.setAdapter(concernAdapter);
            }
            isPause = false;
        }
    }

    @Override
	protected void onDestroy() {
		super.onDestroy();
	    AppController.canclePendingRequests(TAG);
		String uuid = TokenUtils.getDeviceId(this);
		JSONArray data = new JSONArray();
		for (ConcernInfo concernInfo : concernManager.getConcernGame()) {
			data.put(concernInfo.getId());
		}
		ConcernUtils.updateConcernData(Config.HOST + "v2d0/device/" + uuid + "/concern", data,
				new ConcernUtils.DownJsonListener() {
					@Override
					public void downSucced(String str) {
						Utils.log("更新设备关注游戏成功");
					}

					@Override
					public void downFailed() {
						Utils.log("更新设备关注游戏失败");
					}
				});
		view = null;
		concern_rv_show = null;
		concern_rv_recommend = null;
		concern_rl_title = null;
	    reuse_none_data = null;
	}
}
