package com.gh.gamecenter.personal;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.gh.base.AppController;
import com.gh.base.BaseFragment;
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.R;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBReuse;

/**
 * Created by LGT on 2016/8/12.
 */
public class ConcernFragment extends BaseFragment {

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

    private RecyclerView fm_concern_rv_show;
    private ConcernFragmentAdapter adapter;

    private boolean isEverpause;

    private DataWatcher dataWatcher = new DataWatcher() {
        @Override
        public void onDataChanged(DownloadEntity downloadEntity) {
            Integer location = adapter.getLocationMap().get(downloadEntity.getName());
            if (location != null) {
                GameEntity detailedEntity = adapter.getGameList().get(location);
                if (detailedEntity != null) {
                    DownloadItemUtils.processDate(getActivity(), detailedEntity, downloadEntity,
                            adapter, location);
                }
            }
        }
    };

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

        isEverpause = false;

        init(R.layout.fm_concern);

        fm_concern_rv_show.setHasFixedSize(true);
        fm_concern_rv_show.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new ConcernFragmentAdapter(getActivity(), fm_concern_rv_show);
        fm_concern_rv_show.setAdapter(adapter);
    }

    // 关注事件
    public void onEventMainThread(EBConcernChanged changed) {
//        if (changed.isConcern()) {
//
//        } else {
//            for (int i = 0; i < adapter.getGameList().size(); i++) {
//                if (adapter.getGameList().get(i).getId().equals(changed.getGameId())) {
//                    adapter.getGameList().remove(i);
//                    adapter.notifyDataSetChanged();
//                    break;
//                }
//            }
//        }
        adapter = new ConcernFragmentAdapter(getActivity(), fm_concern_rv_show);
        fm_concern_rv_show.setAdapter(adapter);
    }

    //连接上网络事件
    public void onEventMainThread(EBNetworkState busNetworkState) {
        if (busNetworkState.isNetworkConnected()) {

        }
    }

    // 打开下载按钮事件
    public void onEventMainThread(EBReuse reuse) {
        if ("Refresh".equals(reuse.getType()) && adapter != null) {
            adapter.notifyItemRangeChanged(0, adapter.getItemCount());
        }
    }

    //安装、卸载事件
    public void onEventMainThread(EBPackage busFour) {
        String name = adapter.getNameMap().get(busFour.getPackageName());
        if (name != null) {
            Integer location = adapter.getLocationMap().get(name);
            if (location != null) {
                adapter.notifyItemChanged(location);
            }
        }
    }

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

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        AppController.canclePendingRequests(TAG);
    }

}
