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.ImageView;
import android.widget.TextView;

import com.android.views.popup.BasePopupWindow;

import ai.pai.client.R;
import ai.pai.client.activity.GroupActivity;
import ai.pai.client.utils.GroupCreateUtils;

/**
 * Created by chengzhenyu on 2016/3/4.
 */
public class GroupCreatePopup extends BasePopupWindow implements View.OnClickListener{

    private View popupView;
    private String groupName;
    private int   avatarId;

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

    @Override
    protected Animation getShowAnimation() {
        return getDefaultScaleAnimation();
    }

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

    @Override
    public View getPopupView() {
        popupView= LayoutInflater.from(mContext).inflate(R.layout.popup_create_group,null);
        TextView groupNameTextView = (TextView)popupView.findViewById(R.id.tv_pop_create_name);
        ImageView groupAvatarImgView = (ImageView)popupView.findViewById(R.id.iv_group_create_avatar);
        avatarId = GroupCreateUtils.getRandomSeqForCreateGroup();
        groupName=GroupCreateUtils.getGroupName(mContext,avatarId);
        groupNameTextView.setText(groupName);
        groupAvatarImgView.setImageResource(GroupCreateUtils.getDrawableIdForAvatar(avatarId));
        return popupView;
    }

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

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

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_cancel_create:
                dismiss();
                break;
            case R.id.btn_create_group:
                dismiss();
                Intent intent = new Intent(mContext, GroupActivity.class);
                intent.putExtra("name",groupName);
                intent.putExtra("avatar",avatarId);
                mContext.startActivity(intent);
                break;
        }
    }
}
