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.Button;
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.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 Button printQRBtn;
    private ImageView groupQRImg;
    private TextView printerSetText;
    private String groupId;
    private Context context;
    private View.OnClickListener listener;

    public SessionQRPopup(Activity context, String groupId,View.OnClickListener listener) {
        super(context);
        this.groupId = groupId;
        this.context = context;
        this.listener = listener;
        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);
        printQRBtn = ButterKnife.findById(popupView,R.id.btn_print_qr);
        printerSetText = ButterKnife.findById(popupView,R.id.tv_printer_set);

        return popupView;
    }

    private void init(){
        printQRBtn.setOnClickListener(listener);
        printerSetText.setOnClickListener(listener);

        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);
    }
}
