package ai.pai.lensman.main;

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.ProgressBar;
import android.widget.TextView;

import com.android.common.utils.DeviceUtils;
import com.android.common.utils.LogHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

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

import ai.pai.lensman.R;
import ai.pai.lensman.bean.PhotoBean;
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 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 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) {
        LogHelper.d("czy","onBindViewHolder at position ="+position);
        if(sessionList==null){
            return;
        }
        final SessionBean item = sessionList.get(position);

        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));
        ArrayList<PhotoBean> photoList = item.sessionPhotos;

        if(photoList!=null && photoList.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 uploaded = 0;
            int error = 0;
            for(PhotoBean bean : photoList){
                if(bean.uploadStatus == PhotoBean.UploadStatus.STATUS_SUCCESS){
                    uploaded++;
                }
                if(bean.uploadStatus == PhotoBean.UploadStatus.STATUS_ERROR){
                    error++;
                }
            }
            holder.uploadProgressBar.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_upload_bg));
            if(error>0){
                holder.uploadStatus.setText("("+uploaded+"/"+photoList.size()+") "+context.getString(R.string.upload_error));
                holder.uploadProgressBar.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_upload_fail_bg));
            }else if(uploaded<photoList.size()){
                holder.uploadStatus.setText("("+uploaded+"/"+photoList.size()+") "+context.getString(R.string.upload_processing));
            }else{
                holder.uploadStatus.setText("("+uploaded+"/"+photoList.size()+") "+context.getString(R.string.upload_success));
            }
            holder.uploadProgressBar.setMax(photoList.size());
            holder.uploadProgressBar.setProgress(uploaded);

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

        @BindView(R.id.tv_session_upload_status) TextView uploadStatus;

        @BindView(R.id.pb_session_upload)  ProgressBar uploadProgressBar;

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

