package ai.pai.client.views;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.android.views.popup.BasePopupWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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.utils.GroupCreateUtils;

public class GroupSelectPopup extends BasePopupWindow  implements View.OnClickListener,AdapterView.OnItemClickListener{

    private View popupView;
    private ArrayList<GroupInfo> groupInfoList;

    public GroupSelectPopup(Activity context) {
        super(context);
        bindEvent();
    }

    @Override
    protected Animation getShowAnimation() {
        return getTranslateAnimation(250*2,0,200);
    }

    @Override
    protected View getClickToDismissView() {
        return popupView.findViewById(com.android.views.R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView= LayoutInflater.from(mContext).inflate(R.layout.popup_select_group,null);
        ListView groupListView =(ListView) popupView.findViewById(R.id.list_recent_group);
        groupInfoList = DBService.getInstance(mContext).getRecentGroupList(3);
        List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
        for(GroupInfo info :groupInfoList){
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("avatar", GroupCreateUtils.getDrawableIdForAvatar(info.groupAvatarId));
            map.put("name",info.groupName);
            list.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(mContext,list,R.layout.group_list_item,
                new String[]{"avatar","name"},new int[]{R.id.iv_group_avatar,R.id.tv_group_name});
        groupListView.setAdapter(adapter);
        groupListView.setOnItemClickListener(this);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView!=null){
            popupView.findViewById(R.id.tv_pop_cancel_group).setOnClickListener(this);
            popupView.findViewById(R.id.tv_pop_create_group).setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_pop_cancel_group:
                dismiss();
                break;
            case R.id.tv_pop_create_group:
                dismiss();
                new GroupCreatePopup(mContext).showPopupWindow();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        dismiss();
        Intent intent = new Intent(mContext, GroupActivity.class);
        intent.putExtra("group",groupInfoList.get(position));
        mContext.startActivity(intent);
    }
}
