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

import com.android.common.utils.TimeUtils;
import com.android.views.swipeLayout.SwipeLayout;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.GroupActivity;
import ai.pai.client.beans.GroupInfo;
import ai.pai.client.db.DBService;
import ai.pai.client.db.Preferences;
import ai.pai.client.utils.GroupCreateUtils;

public class HistoryGroupAdapter extends RecyclerView.Adapter<HistoryGroupAdapter.GroupViewHolder> {

    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<GroupInfo> groupList;
    private GroupActionListener listener;

    public HistoryGroupAdapter(Context context, ArrayList<GroupInfo> groupList){
        this.context = context;
        this.groupList = groupList;
        mInflater = LayoutInflater.from(context);
    }

    public void addGroupActionListener(GroupActionListener listener){
        this.listener = listener;
    }
    @Override
    public GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.history_group_item,parent,false);
        GroupViewHolder viewHolder = new GroupViewHolder(view);
        viewHolder.avatar = (ImageView)view.findViewById(R.id.iv_group_avatar);
        viewHolder.name = (TextView)view.findViewById(R.id.tv_group_name);
        viewHolder.time = (TextView)view.findViewById(R.id.tv_group_create_time);
        viewHolder.delete = (TextView)view.findViewById(R.id.tv_trash);
        viewHolder.contentView = view.findViewById(R.id.history_group_item_view);
        viewHolder.swipeLayout = (SwipeLayout) view;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final GroupViewHolder holder, int position) {
        final GroupInfo info = groupList.get(position);
        holder.time.setText(TimeUtils.getFormattedTime(info.createTime));
        holder.name.setText(info.groupName);
        holder.avatar.setImageResource(GroupCreateUtils.getDrawableIdForAvatar(info.groupAvatarId));
        holder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GroupActivity.class);
                intent.putExtra("group",info);
                context.startActivity(intent);
            }
        });
        holder.swipeLayout.close();
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    if(info.groupPhotoNum>0){
                        Toast.makeText(context,R.string.group_photo_not_null,Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(!info.adminId.equals(Preferences.getInstance(context).getUserId())){
                        Toast.makeText(context,R.string.group_admin_not_me,Toast.LENGTH_SHORT).show();
                        return;
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }

                groupList.remove(holder.getAdapterPosition());
                notifyItemRemoved(holder.getAdapterPosition());
                DBService.getInstance(context).deleteGroupRelated(info.groupId);
                if(listener!=null){
                    listener.onGroupDeleted(info);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return groupList==null ? 0 : groupList.size();
    }

    public synchronized void clearGroupList(){
        groupList.clear();
        notifyDataSetChanged();
    }

    public synchronized void addGroupList(ArrayList<GroupInfo> groupList){
        this.groupList.addAll(groupList);
        notifyDataSetChanged();
    }

    public ArrayList<GroupInfo> getGroupList(){
        return groupList;
    }

    static class GroupViewHolder extends RecyclerView.ViewHolder{

        private ImageView avatar;
        private TextView name;
        private TextView time;
        private View  contentView;
        private TextView delete;
        private SwipeLayout swipeLayout;

        public GroupViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface GroupActionListener {

        void onGroupDeleted(GroupInfo groupInfo);

    }
}
