package ai.pai.client.adapter;

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.android.common.utils.TimeUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.PhotoDetailsActivity;
import ai.pai.client.beans.GroupPhotoItem;
import ai.pai.client.utils.PhotoLoader;

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

    private Context context;
    private LayoutInflater mInflater;
    private DisplayImageOptions photoOptions;
    private DisplayImageOptions userAvatarOptions;
    private ArrayList<GroupPhotoItem> photoList;
    private int width;

    public PhotoStaggeredAdapter(Context context, ArrayList<GroupPhotoItem> photoList){
        this.context = context;
        this.photoList = photoList;
        width = DeviceUtils.getScreenWidth(this.context)/2;
        mInflater = LayoutInflater.from(this.context);
        photoOptions = PhotoLoader.getPhotoOptions();
        userAvatarOptions = PhotoLoader.getUserAvatarOptions();
    }

    public synchronized void addGroupPhotoItem(GroupPhotoItem item){
        if(!photoList.contains(item)){
            photoList.add(0,item);
            notifyItemInserted(0);
        }
    }

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

    public synchronized void refreshGroupPhotoItems(ArrayList<GroupPhotoItem> items){
        if(items!=null){
            synchronized (photoList){
                for(int position = 0; position < photoList.size(); position++){
                    for(GroupPhotoItem refreshItem : items){
                        GroupPhotoItem item = photoList.get(position);
                        if(refreshItem.photoId.equals(item.photoId)){
                            if(item.thumbupNum!=refreshItem.thumbupNum || item.commentNum != refreshItem.commentNum){
                                item.thumbupNum = refreshItem.thumbupNum;
                                item.commentNum = refreshItem.commentNum;
                                notifyItemChanged(position);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.stagger_photo_item_view,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        viewHolder.photo = (ImageView)view.findViewById(R.id.iv_stagger_photo);
        viewHolder.avatar = (ImageView)view.findViewById(R.id.iv_stagger_avatar);
        viewHolder.commentCount = (TextView)view.findViewById(R.id.tv_stagger_comment_count);
        viewHolder.name = (TextView)view.findViewById(R.id.tv_stagger_name);
        viewHolder.thumbUpCount = (TextView)view.findViewById(R.id.tv_stagger_like_count);
        viewHolder.time = (TextView)view.findViewById(R.id.tv_stagger_time);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final GroupPhotoItem item = photoList.get(position);
        int height ;
        if(item.thumbnailWidth>0){
            height =item.thumbnailHeight *width/item.thumbnailWidth;
        }else{
            height = width*16/10;
        }
        ViewGroup.LayoutParams lp=holder.photo.getLayoutParams();
        lp.height = height;
        holder.photo.setLayoutParams(lp);
        holder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoDetailsActivity.class);
                ArrayList<GroupPhotoItem> groupPhotoList = new ArrayList<>();
                if(photoList.size()<300){
                    groupPhotoList.addAll(photoList);
                }else{
                    if(position>10){
                        groupPhotoList.addAll(photoList.subList(position-9,position+290>photoList.size()?photoList.size():position+290));
                    }else{
                        groupPhotoList.addAll(photoList.subList(0,300));
                    }
                }
                intent.putExtra("photo_item",item);
                intent.putExtra("list",groupPhotoList);
                intent.putExtra("fromGroup",true);
                context.startActivity(intent);
            }
        });
        PhotoLoader.getInstance(context).displayImage(item.thumbnailUrl,holder.photo, photoOptions);
        PhotoLoader.getInstance(context).displayImage(item.uploaderAvatar,holder.avatar, userAvatarOptions);
        holder.name.setText(item.uploaderName);
        holder.time.setText(TimeUtils.getFormattedTime(item.captureTime));
        holder.thumbUpCount.setText(String.valueOf(item.thumbupNum));
        holder.commentCount.setText(String.valueOf( item.commentNum));

    }

    @Override
    public int getItemCount() {
        return photoList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView photo;
        private ImageView avatar;
        private TextView name;
        private TextView time;
        private TextView thumbUpCount;
        private TextView commentCount;

        public MyViewHolder(View view){
            super(view);
        }
    }
}

