package ai.pai.lensman.session;

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 com.android.common.utils.DeviceUtils;

import java.util.ArrayList;

import ai.pai.lensman.R;
import butterknife.BindView;
import butterknife.ButterKnife;

public class PhotoRecyclerAdapter extends RecyclerView.Adapter<PhotoRecyclerAdapter.MyViewHolder> {

    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<PhotoBean> photoList;
    private int width;

    public PhotoRecyclerAdapter(Context context){
        this.context = context;
        width = DeviceUtils.getScreenWidth(this.context);
        mInflater = LayoutInflater.from(this.context);
    }

    public synchronized void addPhotoBean(PhotoBean item){
        if(photoList==null){
            photoList = new ArrayList<>();
        }
        if(!photoList.contains(item)){
            photoList.add(0,item);
            notifyItemInserted(0);
        }
    }

    public synchronized void addPhotoBeans(ArrayList<PhotoBean> items){
        if(photoList==null){
            photoList = new ArrayList<>();
        }
        for(int k = items.size()-1;k>=0;k--){
            if(!photoList.contains(items.get(k))){
                photoList.add(0,items.get(k));
                notifyItemInserted(0);
            }
        }
    }


    public ArrayList<PhotoBean> getPhotoList(){
        return photoList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return  new MyViewHolder(mInflater.inflate(R.layout.photo_item,parent,false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(photoList==null){
            return;
        }
        final PhotoBean item = photoList.get(position);
        holder.photo.setImageResource(R.drawable.demo1);
        int height = width*10/16;
        ViewGroup.LayoutParams lp=holder.photo.getLayoutParams();
        lp.width = width;
        lp.height = height;
        holder.photo.setLayoutParams(lp);
        holder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public int getItemCount() {
        if(photoList==null){
            return 0;
        }
        return photoList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.iv_session_photo_item) ImageView photo;

        public MyViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }
}

