package com.miya.fastcashier.log;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.miya.fastcashier.R;
import com.miya.print.utils.QRCodeUtil;

import androidx.annotation.NonNull;

public class CustomImageCenterDialog extends Dialog{

    private TextView tvOk;
    private TextView tvHint;
    private ImageView ivContent;

    boolean isFullScreen;
    private String mImageContent;
    private String mHintContentText;
    private String mConfirmContent;
    private Bitmap mQrCodeBitmap;

    /**
     * 设置是否全屏显示
     *
     * @param isFullScreen
     * @return
     */
    public CustomImageCenterDialog setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
        return this;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (isFullScreen) {
            setFullScreen();
        }
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void setFullScreen() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION//暂时不需要hideNavigation
//                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(uiOptions);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    public CustomImageCenterDialog(@NonNull Context context) {
        super(context, R.style.CommonDialog);
        initWindows();
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.dialog_center_image);
        setCancelable(true);
        initView();
    }

    private void initView() {
        tvOk = findViewById(R.id.tvOk);
        tvHint = findViewById(R.id.tvSubContent);
        ivContent = findViewById(R.id.ivContent);
    }


    public CustomImageCenterDialog setQrContent(String msg) {
        mImageContent = msg;
        return this;
    }

    public CustomImageCenterDialog setConfirmContent(String msg) {
        mConfirmContent = msg;
        return this;
    }

    public CustomImageCenterDialog setHintContent(String msg) {
        mHintContentText = msg;
        return this;
    }

    /**
     * 创建dialog
     *
     * @return
     */
    public CustomImageCenterDialog build() {
        if (!TextUtils.isEmpty(mConfirmContent)){
            tvOk.setText(mConfirmContent);
        }
        if (!TextUtils.isEmpty(mHintContentText)){
            tvHint.setText(mHintContentText);
        }
        if (!TextUtils.isEmpty(mImageContent)){
            mQrCodeBitmap = QRCodeUtil.createQRCodeBitmap(mImageContent, SizeUtils.dp2px(300));
            ivContent.setImageBitmap(mQrCodeBitmap);
        }
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mQrCodeBitmap != null) {
                    mQrCodeBitmap.recycle();
                }
                dismiss();
            }
        });

        if (isFullScreen) {
            setFullScreen();
        }
        return this;
    }

    /**
     * 初始化对话框(供子类选择调用)
     */
    protected void initWindows() {
        Window win = this.getWindow();
        win.requestFeature(Window.FEATURE_NO_TITLE);
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
//        lp.windowAnimations = R.style.LightInAndOutStyle;
        lp.gravity = Gravity.CENTER;
        win.setAttributes(lp);
        win.setBackgroundDrawableResource(android.R.color.transparent);
    }
}
