package ai.pai.lensman.upload;

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.TextView;

import com.android.common.utils.DeviceUtils;

import java.util.ArrayList;

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

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

    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<SessionBean> sessionList;
    private int width;

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

    public synchronized void addSessionBean(SessionBean item){
        if(sessionList==null){
            sessionList = new ArrayList<>();
        }
        if(!sessionList.contains(item)){
            sessionList.add(0,item);
            notifyItemInserted(0);
        }
    }

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


    public ArrayList<SessionBean> getSessionList(){
        return sessionList;
    }

    public void clear(){
        if(sessionList==null){
            sessionList = new ArrayList<>();
            return;
        }
        sessionList.clear();
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(sessionList==null){
            return;
        }
        final SessionBean item = sessionList.get(position);
        holder.photo.setImageResource(R.drawable.demo1);
        int height = width;
        ViewGroup.LayoutParams lp=holder.photo.getLayoutParams();
        lp.width = width;
        lp.height = height;
        holder.photo.setLayoutParams(lp);
        holder.sesseionSeq.setText(String.valueOf(item.sessionSeq));
        holder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{

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

        @BindView(R.id.tv_session_seq) TextView sesseionSeq;

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

