package ai.pai.lensman.session;

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 android.widget.Toast;

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

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

import ai.pai.lensman.App;
import ai.pai.lensman.R;
import ai.pai.lensman.bean.PhotoBean;
import ai.pai.lensman.db.DBService;
import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.service.UploadService;
import ai.pai.lensman.utils.Constants;
import ai.pai.lensman.utils.ImageLoaderUtils;
import butterknife.BindView;
import butterknife.ButterKnife;

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

    private int width;
    private Context context;
    private LayoutInflater mInflater;
    private  DisplayImageOptions options;
    private ArrayList<PhotoBean> photoList;
    private int delay = 10;
    public PhotoRecyclerAdapter(Context context){
        this.context = context;
        width = DeviceUtils.getScreenWidth(context);
        mInflater = LayoutInflater.from(context);
        options = ImageLoaderUtils.getOptions(R.drawable.default_img);
        delay = Preferences.getInstance().getUploadDelay();
    }

    public PhotoBean getPhotoAt(int position){
        if(photoList==null|| photoList.size()<=position ||position<0){
            return null;
        }
        return photoList.get(position);
    }

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

    public synchronized void addPreviousPhotos(ArrayList<PhotoBean> photos){
        if(photos==null||photos.size()==0){
            return;
        }
        if(photoList==null){
            photoList = new ArrayList<>();
        }
        if(photoList.size()==0){
            photoList.addAll(photos);
            notifyDataSetChanged();
        }
    }

    public synchronized void removePhotoAtIndex(int index){
        if(photoList==null || photoList.size()<=index){
            return;
        }
        photoList.remove(index);
        notifyItemRemoved(index);
        notifyItemRangeChanged(0,photoList.size());
    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if(photoList==null){
            return;
        }
        final PhotoBean item = photoList.get(position);
        ImageLoaderUtils.displayLocalImage(item.photoPath, holder.photo, options);
        int height = width*10/16;
        ViewGroup.LayoutParams lp=holder.photo.getLayoutParams();
        lp.width = width;
        lp.height = height;
        holder.photo.setLayoutParams(lp);
        if(item.uploadStatus == PhotoBean.UploadStatus.STATUS_ERROR){
            holder.errorLayout.setVisibility(View.VISIBLE);
            holder.retryImg.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(App.getAppContext(), UploadService.class);
                    intent.putExtra("photo",item);
                    App.getAppContext().startService(intent);
                    item.uploadStatus = PhotoBean.UploadStatus.STATUS_NO_BEGIN;
                    notifyItemChanged(position);
                    Toast.makeText(context,R.string.add_to_upload_queue,Toast.LENGTH_SHORT).show();
                }
            });
        }else{
            holder.errorLayout.setVisibility(View.GONE);
        }
        long timeLeft = delay-(System.currentTimeMillis()-item.captureTime)/1000;

        if(timeLeft<0|| DBService.getInstance().isPhotoUploaded(item.photoId)){
            item.canDelete = false;
            holder.autoUploadHintText.setVisibility(View.GONE);
        }else{
            item.canDelete = true;
            holder.autoUploadHintText.setVisibility(View.VISIBLE);
            holder.autoUploadHintText.setText(context.getString(R.string.auto_upload_after_seconds,timeLeft));
        }
    }

    @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;
        @BindView(R.id.layout_upload_fail) View errorLayout;
        @BindView(R.id.iv_upload_retry)  ImageView retryImg;
        @BindView(R.id.tv_auto_upload_hint) TextView autoUploadHintText;
        public MyViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }
}

