package ai.pai.lensman.session;

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 com.android.common.utils.DeviceUtils;
import com.android.views.popup.BasePopupWindow;

import ai.pai.lensman.R;
import ai.pai.lensman.qrcode.QRCreateUtils;
import ai.pai.lensman.utils.UrlContainer;
import butterknife.ButterKnife;

public class SessionQRPopup extends BasePopupWindow {

    private View popupView;
    private ImageView groupQRImg;
    private String groupId;
    private Context context;


    public SessionQRPopup(Activity context, String groupId) {
        super(context);
        this.groupId = groupId;
        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_session_qr, null);
        groupQRImg = ButterKnife.findById(popupView,R.id.iv_group_qrcode);
        return popupView;
    }

    private void init(){
        try{
            initQRImageSize();
            groupQRImg.setImageBitmap(QRCreateUtils.Create2DCode(UrlContainer.HOST_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);
    }
}
