package ai.pai.lensman.upload;

import android.content.Context;
import android.content.Intent;
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 com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.io.File;
import java.util.ArrayList;

import ai.pai.lensman.R;
import ai.pai.lensman.bean.SessionBean;
import ai.pai.lensman.session.SessionActivity;
import ai.pai.lensman.utils.Constants;
import ai.pai.lensman.utils.ImageLoaderUtils;
import butterknife.BindView;
import butterknife.ButterKnife;

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

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

    public SessionRecyclerAdapter(Context context){
        this.context = context;
        width = DeviceUtils.getScreenWidth(this.context)/2;
        mInflater = LayoutInflater.from(this.context);
        options = ImageLoaderUtils.getOptions(R.drawable.default_img);
    }

    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);
        if(item.sessionPhotos!=null && item.sessionPhotos.size()>0){
            String path =  Constants.APP_IMAGE_DIR + File.separator+item.sessionId+File.separator+Constants.THUMBNAIL_DIR_NAME+File.separator+item.sessionPhotos.get(0).photoName;
            ImageLoaderUtils.displayLocalImage(path, holder.photo, options);
        }
        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) {
                Intent intent = new Intent(context, SessionActivity.class);
                intent.putExtra("session",item);
                context.startActivity(intent);
            }
        });
    }

    @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);
        }
    }
}

