package com.mjsheng.myappstore.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.image.ImageLoader;

import java.util.List;

public class TopRecyclerViewAdapter extends RecyclerView.Adapter<TopRecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private List<String> mDatas;
    private HorizontalScrollViewListener listener;
    private String title;

    public TopRecyclerViewAdapter(Context context, String title, List<String> datas, HorizontalScrollViewListener listener) {
        this.mContext = context;
        this.title = title;
        this.mDatas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View v = null;
        if (title.equals("home")) {
            v = LayoutInflater.from(mContext).inflate(
                    R.layout.scrollview_home_fragment, parent, false);
        } else {
            v = LayoutInflater.from(mContext).inflate(
                    R.layout.scrollview_appdetail_page, parent, false);
        }
        v.setLayoutParams(layoutParams);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final String iconUrl = mDatas.get(position);
        if (iconUrl != null) {
            ImageLoader.getDefault().loadUrl(mContext, iconUrl,  holder.mImg);
        }

        holder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(iconUrl,position);
            }
        });
    }

    public long getItemId(int position) {
        return position;
    }

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


    public interface HorizontalScrollViewListener {
        void onClick(String iconUrl, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImg;

        public ViewHolder(final View itemView) {
            super(itemView);
            mImg = (ImageView) itemView.findViewById(R.id.image_iv);
        }
    }
}
