package com.gh.gamecenter.gamedetail.fuli.tools;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gh.common.constant.Config;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.NewsDetailActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.WebActivity;
import com.gh.gamecenter.adapter.viewholder.ToolBoxViewHolder;
import com.gh.gamecenter.databinding.ToolboxItemBinding;
import com.gh.gamecenter.common.entity.ToolBoxEntity;
import com.lightgame.adapter.BaseRecyclerAdapter;

import java.util.List;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by khy on 25/05/17.
 */

public class GameDetailToolsAdapter extends BaseRecyclerAdapter {
    private boolean isExpand = false;
    private int mShowItemCount = 3;
    private List<ToolBoxEntity> mToolBoxList;
    private String mGameName;
    private final int MORE = 0;
    private final int TOOLBOX = 1;
    private int mItemColor = R.color.white;

    public void setItemColor(@ColorRes int color) {
        mItemColor = color;
    }

    public GameDetailToolsAdapter(Context context, String gameName, List<ToolBoxEntity> toolBoxList) {
        super(context);
        mToolBoxList = toolBoxList;
        mGameName = gameName;
    }

    @Override
    public int getItemViewType(int position) {
        if (mToolBoxList.size() > 3 && !isExpand) {
            if (position == mShowItemCount) {
                return MORE;
            } else {
                return TOOLBOX;
            }
        } else {
            return TOOLBOX;
        }
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TOOLBOX) {
            View view = mLayoutInflater.inflate(R.layout.toolbox_item, parent, false);
            return new ToolBoxViewHolder(ToolboxItemBinding.bind(view));
        } else {
            return new MoreViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_game_detail_more, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ToolBoxViewHolder) {
            ToolBoxEntity toolBoxEntity = mToolBoxList.get(position);
            ToolBoxViewHolder viewHolder = (ToolBoxViewHolder) holder;
            initToolBoxViewHolder(viewHolder, toolBoxEntity);
        } else {
            holder.itemView.setOnClickListener(v -> {
                MtaHelper.onEvent("游戏详情_新", "游戏工具-展开", mGameName);
                isExpand = true;
                notifyItemRangeInserted(mShowItemCount + 1, mToolBoxList.size() - mShowItemCount);
            });
        }
    }

    @Override
    public int getItemCount() {
        return (mToolBoxList.size() > 3 && !isExpand) ? mShowItemCount + 1 : mToolBoxList.size();
    }

    private void initToolBoxViewHolder(ToolBoxViewHolder viewHolder, final ToolBoxEntity toolBoxEntity) {
        viewHolder.itemView.setBackground(ContextCompat.getDrawable(mContext, mItemColor));
        viewHolder.itemView.setPadding(0, DisplayUtils.dip2px(mContext, 10), 0, DisplayUtils.dip2px(mContext, 10));

        viewHolder.binding.toolboxItemDes.setText(toolBoxEntity.getDes());
        viewHolder.binding.toolboxItemTitle.setText(toolBoxEntity.getName());
        ImageUtils.display(viewHolder.binding.toolboxItemGameThumb, toolBoxEntity.getIcon());

        viewHolder.itemView.setOnClickListener(v -> {
            String url = toolBoxEntity.getUrl();

            if (url != null && url.contains(Config.URL_ARTICLE)) {
                // 写个注释例子， http://www.ghzs666.com/article/${articleId}.html
                String newsId = url.substring(url.lastIndexOf("/") + 1, url.length() - ".html".length());
                Intent intent = NewsDetailActivity.getIntentById(mContext, newsId, "游戏详情-工具箱");
                mContext.startActivity(intent);
            } else {
                mContext.startActivity(WebActivity.getWebByCollectionTools(mContext, toolBoxEntity, false));
            }
        });
    }

}

class MoreViewHolder extends RecyclerView.ViewHolder {

    MoreViewHolder(@NonNull View itemView) {
        super(itemView);
    }
}
