package com.uiui.zyappstore.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.activity.KindDetailActivity;
import com.uiui.zyappstore.bean.Kind;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by asus on 2017/10/25.
 */

public class KindAdapter extends RecyclerView.Adapter<KindAdapter.KindViewHolder> {

    private List<Kind> mKindList;
    private Context mContext;

    public KindAdapter(List<Kind> mKindList, Context context) {
        this.mKindList = mKindList;
        this.mContext = context;
    }

    @Override
    public KindViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new KindViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_kind, parent, false));
    }

    @Override
    public void onBindViewHolder(KindViewHolder holder, int position) {
        final Kind kind = mKindList.get(position);
        if (kind.getHide() == 1) {
            setVisibility( false,holder.root);
        }else {
            setVisibility( true,holder.root);
        }
        holder.tvName.setText(kind.getName());
        Glide.with(mContext).load(kind.getIcon())
//                .transform(new GlideCircleWithBorder(mContext))
                .into(holder.ivIcon);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                KindDetailActivity.startActivity(mContext, kind.getCategory(), kind.getName());
                Intent intent = new Intent(mContext, KindDetailActivity.class);
                intent.putExtra("kind", kind.getCategory());
                intent.putExtra("name", kind.getName());
                mContext.startActivity(intent);

            }
        });

    }
    // 防止隐藏item出现空白
    public void setVisibility(boolean isVisible, View itemView) {
        RecyclerView.LayoutParams param = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (isVisible) {
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            itemView.setVisibility(View.VISIBLE);
        } else {
            param.height = 0;
            param.width = 0;
            itemView.setVisibility(View.GONE);
        }
        itemView.setLayoutParams(param);
    }

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

    public static class KindViewHolder extends RecyclerView.ViewHolder {
        ImageView ivIcon, ivDetail;
        TextView tvName;
        LinearLayout root;

        public KindViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.kind_iv_icon);
            ivDetail = itemView.findViewById(R.id.kind_iv_detail);
            tvName = itemView.findViewById(R.id.kind_tv_name);
            root = itemView.findViewById(R.id.root);
        }
    }


}
