package com.rv.home.rv.module.ui.main.home.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.utils.ui.datetime.selector.util.TextUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.ruiwenliu.wrapper.util.TransformationUtil2;
import com.ruiwenliu.wrapper.util.glide.GlideOptions;
import com.rv.home.R;
import com.rv.home.rv.module.ui.main.home.bean.HomeHotGoodListBean;

import java.util.List;

/**
 * Created :Auser
 * Date: 2019/5/17.
 * Desc:热门车型适配
 */

public class HotCarTypeListAdapter extends BaseAdapter {
    private Context mContext ;
    private List<HomeHotGoodListBean.DataBean> beans ;
    private String hotType ;

    public HotCarTypeListAdapter(Context context,String hotType, List<HomeHotGoodListBean.DataBean> beans){
        this.mContext = context ;
        this.beans = beans ;
        this.hotType = hotType ;
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int i) {
        return beans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        HomeHotGoodListBean.DataBean bean = beans.get(i);
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.rv_item_hot_car_type, null);
            holder = new ViewHolder();
            holder.iv_goods = view.findViewById(R.id.iv_goods);
            holder.tv_name = view.findViewById(R.id.tv_name);
            holder.tv_content = view.findViewById(R.id.tv_content);
            holder.tv_price = view.findViewById(R.id.tv_price);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        GlideOptions options = GlideOptions.placeholderOf(com.ruiwenliu.wrapper.R.drawable.glide_icon_placeholder).
                error(com.ruiwenliu.wrapper.R.drawable.glide_icon_error);
        TransformationUtil2 utils = new TransformationUtil2(holder.iv_goods);
        Glide.with(mContext)
                .asBitmap()
                .load(bean.getIcon())
                .apply(options)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(12)).override(holder.iv_goods.getWidth(), holder.iv_goods.getHeight()))
                .into(utils);

        holder.tv_name.setText(bean.getName());
        holder.tv_content.setText( bean.getName1());

        if (!TextUtil.isEmpty(bean.getPrice())) {
            holder.tv_price.setText(String.format("¥%1$s%2$s", bean.getPrice(), getUnit()));
        } else {
            holder.tv_price.setText( "");
        }

        if ("2".equals(hotType) || "4".equals(hotType)) {
            holder.tv_content.setVisibility(View.GONE) ;
            holder.tv_price.setVisibility(View.VISIBLE);
        } else if("3".equals(hotType)) {
            holder.tv_content.setVisibility(View.GONE);
            holder.tv_price.setVisibility(View.GONE);
        } else{
            holder.tv_content.setVisibility(View.VISIBLE);
            holder.tv_price.setVisibility(View.GONE);
        }
        return null;
    }

    private String getUnit() {
        if (!TextUtils.isEmpty(hotType)) {
            switch (hotType) {
                case "4":
                    return mContext.getString(R.string.rv_one_car);
                case "2":
                    return mContext.getString(R.string.rv_one_person);
                case "3":
                    return mContext.getString(R.string.rv_day);
                default:
                    return mContext.getString(R.string.rv_one_car);
            }
        }
        return mContext.getString(R.string.rv_one_car);
    }

    public void setData(List<HomeHotGoodListBean.DataBean> data) {
        this.beans = data ;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        ImageView iv_goods;
        TextView tv_name;
        TextView tv_content ;
        TextView tv_price;
    }
}
