package ai.pai.client.views;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.common.utils.DeviceUtils;
import com.android.views.popup.BasePopupWindow;

import ai.pai.client.R;
import ai.pai.client.qrcode.QRCreateUtils;
import ai.pai.client.utils.GroupCreateUtils;
import ai.pai.client.utils.UrlContainer;

/**
 * Created by chengzhenyu on 2016/3/4.
 */
public class GroupQRPopup extends BasePopupWindow {

    private View popupView;
    private int avatarId;
    private ImageView groupQRImg;
    private TextView groupNameTextView;
    private ImageView groupAvatarImgView;
    private String groupId;
    private String groupName;
    private Context context;


    public GroupQRPopup(Activity context, String groupId, String groupName, int avatarId) {
        super(context);
        this.groupId = groupId;
        this.avatarId = avatarId;
        this.groupName = groupName;
        this.context = context;
        init();
    }

    @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.pop_group_qr, null);
        groupNameTextView = (TextView) popupView.findViewById(R.id.tv_group_name);
        groupAvatarImgView = (ImageView) popupView.findViewById(R.id.iv_group_avatar);
        groupQRImg = (ImageView) popupView.findViewById(R.id.iv_group_qrcode);
        return popupView;
    }

    private void init(){
        groupNameTextView.setText(groupName);
        groupAvatarImgView.setImageResource(GroupCreateUtils.getDrawableIdForAvatar(avatarId));
        try{
            initQRImageSize();
            groupQRImg.setImageBitmap(QRCreateUtils.Create2DCode(UrlContainer.QR_APP_GROUP_URL+groupId, DeviceUtils.dip2px(context,200)));
            groupQRImg.setVisibility(View.VISIBLE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

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

    private void initQRImageSize() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) groupQRImg.getLayoutParams();
        params.width = DeviceUtils.dip2px(context,200);
        params.height = DeviceUtils.dip2px(context,200);
        groupQRImg.setLayoutParams(params);
    }
}
