package com.gh.gamecenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gh.common.util.DataUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.DownloadItemUtils;
import com.gh.common.util.GameViewUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.view.CardLinearLayout;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.GameDetailActivity;
import com.gh.gamecenter.PluginActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.GameNormalViewHolder;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.GameUpdateEntity;
import com.gh.gamecenter.listener.OnCallBackListener;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.GameManager;
import com.gh.gamecenter.manager.PackageManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LGT on 2016/7/6.
 */
public class PluginAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private OnCallBackListener listener;

    private List<GameEntity> pluginList;
    private ArrayMap<String, Integer> locationMap;
    private ArrayMap<String, String> nameMap;

    private String entrance;

    public PluginAdapter(PluginActivity activity, String entrance) {

        this.context = activity;
        this.listener = activity;
        this.entrance = entrance;

        pluginList = new ArrayList<>();

        locationMap = new ArrayMap<>();
        nameMap = new ArrayMap<>();

        init();
    }

    // 检查可以插件化的游戏
    private void init() {
        ArrayList<GameUpdateEntity> updateList = PackageManager.getUpdateList();
        if (updateList.isEmpty()) {
            return;
        }
        GameUpdateEntity gameUpdateEntity;
        for (int i = 0, size= updateList.size(); i < size; i++) {
            gameUpdateEntity = updateList.get(i);
            if (gameUpdateEntity.isPluggable()) {
                GameEntity gameEntity = new GameEntity();
                gameEntity.setId(gameUpdateEntity.getId());
                gameEntity.setName(gameUpdateEntity.getName());
                gameEntity.setIcon(gameUpdateEntity.getIcon());
                gameEntity.setTag(gameUpdateEntity.getTag());
                gameEntity.setBrief(gameUpdateEntity.getBrief());
                gameEntity.setPluggable(true);

                ApkEntity apkEntity = new ApkEntity();
                apkEntity.setUrl(gameUpdateEntity.getUrl());
                apkEntity.setPackageName(gameUpdateEntity.getPackageName());
                apkEntity.setSize(gameUpdateEntity.getSize());
                apkEntity.setVersion(gameUpdateEntity.getVersion());
                apkEntity.setGhVersion(gameUpdateEntity.getGhVersion());
                apkEntity.setPlatform(gameUpdateEntity.getPlatform());
                apkEntity.setEtag(gameUpdateEntity.getEtag());

                ArrayList<ApkEntity> apk = new ArrayList<>();
                apk.add(apkEntity);
                gameEntity.setApk(apk);

                pluginList.add(gameEntity);
            }
        }
        if (!pluginList.isEmpty()) {
            GameManager manager = new GameManager(context);
            for (GameEntity gEntity : pluginList) {
                gEntity.setEntryMap(DownloadManager.getInstance(context).getEntryMap(gEntity.getName()));
                manager.addOrUpdate(gEntity.getApk(), gEntity.getId(), gEntity.getName());
            }
            notifyItemRangeInserted(0, pluginList.size());
            initLocationMap();
            if (listener != null) {
                listener.loadDone();
            }
        }
    }

    private void initLocationMap() {
        for (int i = 0; i < pluginList.size(); i++) {
            GameEntity gameEntity = pluginList.get(i);
            locationMap.put(gameEntity.getName() + " - " + gameEntity.getApk().get(0).getPlatform(), i);
            for (ApkEntity apkEntity : gameEntity.getApk()) {
                nameMap.put(apkEntity.getPackageName(), gameEntity.getName() + " - " + gameEntity.getApk().get(0).getPlatform());
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.game_normal_item, parent, false);
        return new GameNormalViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof GameNormalViewHolder) {
            final GameNormalViewHolder viewHolder = (GameNormalViewHolder) holder;

            // 第一个
            if (position == 0) {
                ((CardLinearLayout) viewHolder.itemView).setmTop(DisplayUtils.dip2px(context, 8));
            } else {
                ((CardLinearLayout) viewHolder.itemView).setmTop(0);
            }

            // 最后一个
            if (position == pluginList.size() - 1) {
                ((CardLinearLayout) viewHolder.itemView).setBottom(true);
            } else {
                ((CardLinearLayout) viewHolder.itemView).setBottom(false);
            }

            final GameEntity gameEntity = pluginList.get(position);

            viewHolder.home1_game_order.setVisibility(View.GONE);

            ImageUtils.getInstance(context).display(gameEntity.getIcon(), viewHolder.gameThumb);
            if (gameEntity.isPluggable()) {
                viewHolder.gameNameAndSize.setText(gameEntity.getName() + " - " +
                        PlatformUtils.getInstance(context).getPlatformName(gameEntity.getApk().get(0).getPlatform()));
            } else {
                viewHolder.gameNameAndSize.setText(gameEntity.getName());
            }
            if (gameEntity.getApk() == null || gameEntity.getApk().isEmpty()) {
                viewHolder.gameDes.setText(gameEntity.getBrief());
            } else {
                viewHolder.gameDes.setText(gameEntity.getApk().get(0).getSize() + "  " + gameEntity.getBrief());
            }
            GameViewUtils.setLabelList(context, viewHolder.labelList, gameEntity.getTag());

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String, Object> kv = new HashMap<>();
                    kv.put("名字", gameEntity.getName());
                    kv.put("位置", String.valueOf(viewHolder.getPosition() + 1));
                    DataUtils.onEvent(context, "点击", "可以插件化的游戏", kv);

                    Map<String, Object> map = new HashMap<>();
                    map.put("location", "列表");
                    map.put("page", "可以插件化的游戏");
                    map.put("game", gameEntity.getName());
                    map.put("game_id", gameEntity.getId());
                    DataCollectionManager.onEvent(context, "click-item", map);

                    Intent intent = new Intent(context, GameDetailActivity.class);
                    intent.putExtra("gameId", gameEntity.getId());
                    intent.putExtra("entrance", entrance + "+(可以插件化的游戏)");
                    context.startActivity(intent);
                }
            });

            DownloadItemUtils.setOnClickListener(context,  viewHolder.downloadBtn, gameEntity, position,
                    PluginAdapter.this, entrance + "+(可以插件化的游戏)", "可以插件化的游戏:" + gameEntity.getName());

            DownloadItemUtils.updateItem(context, viewHolder.gameDes,
                    viewHolder.game_progressbar,  viewHolder.game_ll_info,  viewHolder.download_speed,
                    viewHolder.download_percentage,  viewHolder.downloadBtn, gameEntity, false);
        }
    }

    @Override
    public int getItemCount() {
        return pluginList.size();
    }

    public List<GameEntity> getPluginList() {
        return pluginList;
    }

    public ArrayMap<String, Integer> getLocationMap() {
        return locationMap;
    }

    public ArrayMap<String, String> getNameMap() {
        return nameMap;
    }

}
