package com.gh.gamecenter.adapter.viewholder;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.airbnb.lottie.LottieAnimationView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.common.view.DrawableView;
import com.gh.gamecenter.feature.databinding.GameItemBinding;
import com.gh.gamecenter.feature.entity.ColorEntity;
import com.gh.gamecenter.feature.entity.GameEntity;
import com.gh.gamecenter.feature.view.DownloadButton;
import com.gh.gamecenter.feature.view.GameIconView;

public class GameViewHolder extends RecyclerView.ViewHolder {

    public GameIconView gameThumb;
    public TextView gameName;
    public DownloadButton gameDownloadBtn;
    public TextView gameDes;
    public LinearLayout gameLabelList;
    @Nullable
    public View recommendContainer;
    public TextView recommendTv;
    public SimpleDraweeView recommendIv;
    public TextView gameServerType;
    public LottieAnimationView gameDownloadTips;
    public TextView multiVersionDownloadTv;

    @Nullable
    public TextView gameRating;

    @Nullable
    public View recommendStarInfo;

    public GameViewHolder(View itemView) {
        super(itemView);
    }

    public GameViewHolder(GameItemBinding binding) {
        super(binding.getRoot());
        gameDownloadBtn = binding.downloadBtn;
        gameDes = binding.gameDes;
        gameDownloadTips = binding.downloadTipsLottie;
        multiVersionDownloadTv = binding.multiVersionDownloadTv;
        gameRating = binding.gameRating;
        recommendContainer = binding.recommendContainer;
        recommendTv = binding.recommendTv;
        recommendIv = binding.recommendIv;
        recommendStarInfo = binding.recommendStarInfo;
    }

    public void initServerType(GameEntity gameEntity) {
        ColorEntity serverLabel = gameEntity.getServerLabel();
        if (gameEntity.getTest() != null) {
            gameServerType.setVisibility(View.GONE);
        } else if (serverLabel != null) {
            gameServerType.setVisibility(View.VISIBLE);
            gameServerType.setText(serverLabel.getValue());
            if (gameEntity.isUseDefaultServerStyle()) {
                gameServerType.setBackground(ExtensionsKt.toDrawable(R.drawable.server_label_default_bg));
                gameServerType.setTextColor(ExtensionsKt.toColor(R.color.text_server_label));
            } else {
                gameServerType.setBackground(DrawableView.getServerDrawable(serverLabel.getColor()));
                gameServerType.setTextColor(ExtensionsKt.toColor(R.color.white));
            }
        } else {
            gameServerType.setVisibility(View.GONE);
        }

        // 由于RecyclerView的复用机制 需要每次测量gameName的宽
        gameName.requestLayout();
    }

}
