package xn.xxp.home.auth;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ClickUtils;

import xn.xxp.R;
import xn.xxp.databinding.DialogPickSignCheckBinding;

public class PickSignCheckDialog extends Dialog {

    private final OnSignTypeSelectedListener listener;
    private DialogPickSignCheckBinding binding;
    private boolean admission;
    private boolean inspection;
    private boolean bigDev;

    /**
     * @param admission  准入
     * @param inspection 巡查
     * @param bigDev     大仪
     */
    public PickSignCheckDialog(@NonNull Context context, boolean admission, boolean inspection, boolean bigDev, OnSignTypeSelectedListener listener) {
        super(context, R.style.AlertDialogStyle);
        this.listener = listener;
        this.admission = admission;
        this.inspection = inspection;
        this.bigDev = bigDev;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogPickSignCheckBinding.inflate(LayoutInflater.from(getContext()));
        setContentView(binding.getRoot());
        initWindowConfig();
        initViews();
    }

    private void initWindowConfig() {
        if (getWindow() != null) {
            getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
            getWindow().setLayout(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            );
        }
    }

    private void initViews() {
        binding.close.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                dismiss();
            }
        });
        if (admission) {
            binding.admissionRL.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    dismiss();
                    listener.onSignTypeSelected(SignType.ACCESS.getCode());
                }
            });
        } else {
            binding.admissionRL.setVisibility(View.GONE);
        }
        if (inspection) {
            binding.inspectionRL.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    dismiss();
                    listener.onSignTypeSelected(SignType.PATROL.getCode());
                }
            });
        } else {
            binding.inspectionRL.setVisibility(View.GONE);
        }
        if (bigDev) {
            binding.bigDevRL.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                @Override
                public void onDebouncingClick(View v) {
                    dismiss();
                    listener.onSignTypeSelected(SignType.BIG_DEV.getCode());
                }
            });
        } else {
            binding.bigDevRL.setVisibility(View.GONE);
        }
    }

    public interface OnSignTypeSelectedListener {
        void onSignTypeSelected(int type);
    }
}