package com.hainaos.vc.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.hainaos.vc.R;


public class PermissionsDialog extends AlertDialog {
    private Context mContext;

    private TextView tv_content;
    private TextView tv_cancel;
    private TextView tv_confirm;

    private String mTitle;
    private String mContent;

    public void setTitle(String title) {
        mTitle = title;
    }

    public void setContent(String content) {
        mContent = content;
    }

    public PermissionsDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    public PermissionsDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    public PermissionsDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public void setPermissionsCallback(PermissionsCallback callback) {
        this.mPermissionsCallback = callback;
    }

    private PermissionsCallback mPermissionsCallback;

    public interface PermissionsCallback {
        void onGranted();

        void onDenied();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_permissions);
        setCanceledOnTouchOutside(false);
        tv_content = findViewById(R.id.tv_content);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_confirm = findViewById(R.id.tv_confirm);

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPermissionsCallback != null) {
                    mPermissionsCallback.onDenied();
                }
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPermissionsCallback != null) {
                    mPermissionsCallback.onGranted();
                }
            }
        });
        setText();
    }

    @Override
    public void show() {
        super.show();
        setText();
    }

    private void setText() {
        if (!TextUtils.isEmpty(mContent)) {
            tv_content.setText(mContent);
        }
    }
}
