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.android.views.circleimageview.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.GroupActivity;
import ai.pai.client.activity.PhotoDetailsActivity;
import ai.pai.client.beans.GroupInfo;
import ai.pai.client.beans.GroupPhotoItem;
import ai.pai.client.utils.GroupCreateUtils;
import ai.pai.client.utils.PhotoLoader;
import ai.pai.client.utils.UmengEvent;

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

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

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

    public synchronized void clearPhotoList(){
        photoList.clear();
        notifyDataSetChanged();
    }

    public synchronized void addPhotoList(ArrayList<GroupPhotoItem> photoList){
        int startPosition = this.photoList.size();
        this.photoList.addAll(photoList);
        notifyItemRangeChanged(startPosition,photoList.size());
    }

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.stagger_recent_photo_item_view,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        viewHolder.photo = (ImageView)view.findViewById(R.id.iv_stagger_photo);
        viewHolder.avatar = (CircleImageView)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);
        viewHolder.headLayout = view.findViewById(R.id.layout_stagger_header);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, 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.photo.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoDetailsActivity.class);
                intent.putExtra("list",photoList);
                intent.putExtra("photo_item",item);
                context.startActivity(intent);
                MobclickAgent.onEvent(context, UmengEvent.home_item_photo_click);
            }
        });
        holder.headLayout.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GroupActivity.class);
                GroupInfo info = new GroupInfo();
                info.groupId = item.groupId;
                info.groupName = item.groupName;
                info.groupAvatarId = item.groupDefaultAvatar;
                intent.putExtra("group",info);
                context.startActivity(intent);
                MobclickAgent.onEvent(context, UmengEvent.home_item_group_header_click);
            }
        });
        PhotoLoader.getInstance(context).displayImage(item.thumbnailUrl,holder.photo,options);
        if(item.groupDefaultAvatar>=0){
            holder.avatar.setImageResource(GroupCreateUtils.getDrawableIdForAvatar(item.groupDefaultAvatar));
        }else{
            PhotoLoader.getInstance(context).displayImage(item.groupAvatar,holder.avatar,options);
        }
        holder.name.setText(item.groupName);
        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 CircleImageView avatar;
        private TextView name;
        private TextView time;
        private TextView thumbUpCount;
        private TextView commentCount;
        private View headLayout;

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

