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.PluginAdapter;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPackage;

import java.util.List;

/**
 * Created by LGT on 2016/7/6.
 */
public class PluginActivity extends BaseActivity {

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

    private RecyclerView plugin_list;
    private PluginAdapter adapter;
    private ProgressBarCircularIndeterminate plugin_pb_loading;
    private LinearLayout reuse_no_connection;

    private String entrance;

    private boolean isEverpause = false;

    private DataWatcher dataWatcher = new DataWatcher() {
        @Override
        public void onDataChanged(DownloadEntity downloadEntity) {
            //特殊 插件化update
            if (downloadEntity.isPluggable()) {
                Integer location = adapter.getLocationMap().get(
                        downloadEntity.getName() + " - " + downloadEntity.getPlatform());
                if (location != null) {
                    GameEntity detailedEntity = adapter.getPluginList().get(location);
                    if (detailedEntity != null) {
                        DownloadItemUtils.processDate(PluginActivity.this, detailedEntity,
                                downloadEntity, adapter, location);
                    }
                }
            }
        }
    };

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

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

        View contentView = View.inflate(this, R.layout.activity_plugin, null);

        init(contentView, "可以插件化的游戏");

        reuse_no_connection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                plugin_pb_loading.setVisibility(View.VISIBLE);
                plugin_list.setVisibility(View.VISIBLE);
                reuse_no_connection.setVisibility(View.GONE);
                adapter = new PluginAdapter(PluginActivity.this, entrance);
                plugin_list.setAdapter(adapter);
            }
        });

        plugin_list.setHasFixedSize(true);
        plugin_list.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PluginAdapter(this, entrance);
        plugin_list.setAdapter(adapter);

    }

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

    //连接上网络事件
    public void onEventMainThread(EBNetworkState busNetworkState) {
        if (busNetworkState.isNetworkConnected()) {
            if (reuse_no_connection.getVisibility() == View.VISIBLE) {
                plugin_list.setVisibility(View.VISIBLE);
                plugin_pb_loading.setVisibility(View.VISIBLE);
                reuse_no_connection.setVisibility(View.GONE);
                adapter = new PluginAdapter(this, entrance);
                plugin_list.setAdapter(adapter);
            }
        }
    }

    //安装、卸载事件
    public void onEventMainThread(EBPackage busFour) {
        //特殊 插件化
        String name = adapter.getNameMap().get(busFour.getPackageName());
        if (name != null) {
            Integer location = adapter.getLocationMap().get(name);
            if (location != null) {
                if ("安装".equals(busFour.getType()) || "卸载".equals(busFour.getType())) {
                    List<GameEntity> list = adapter.getPluginList();
                    for (int i = 0, size = list.size(); i < size; i++) {
                        if (list.get(i).getApk().get(0).getPackageName()
                                .equals(busFour.getPackageName())) {
                            list.remove(i);
                            adapter.notifyItemRemoved(location);
                            break;
                        }
                    }
                }
            }
        }
    }

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

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppController.canclePendingRequests(TAG);
        plugin_list = null;
        adapter = null;
        plugin_pb_loading = null;
        reuse_no_connection = null;
        dataWatcher = null;
    }
}
