package com.ttstd.dialer.fragment.dialog.permission;

import android.Manifest;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ttstd.dialer.R;
import com.ttstd.dialer.base.BaseDialogFragment;
import com.ttstd.dialer.databinding.DialogFragmentPermissionsBinding;

public class PermissionDialogFragment extends BaseDialogFragment {
    private static final String TAG = "PermissionDialog";
    private static final int REQUEST_CODE_CALL = 7897;

    private View rootView;
    private FragmentActivity mContext;
    private DialogFragmentPermissionsBinding mBinding;
    private String mContent;

    public PermissionDialogFragment(String content) {
        this.mContent = content;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.e(TAG, "onCreateView: ");
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_fragment_permissions, container, false);
        mBinding.setClick(new BtnClick());
        rootView = mBinding.getRoot();
        mContext = getActivity();
        initView();
        return rootView;
    }

    private void initView() {
        mBinding.tvContent.setText(mContent);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window == null) return;
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getDialog().setCancelable(true);
            getDialog().setCanceledOnTouchOutside(true);
        }
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        DialogFragment fragment = (DialogFragment) manager.findFragmentByTag(tag);
        if (fragment != null && fragment.isAdded()
                && fragment.getDialog() != null && fragment.getDialog().isShowing()) {
            return;
        }

        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            Log.e(TAG, "show: " + e.getMessage());
        }
    }


    @Override
    public void fetchData() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_CALL) {

            dismiss();
        }
    }

    public class BtnClick {
        public void onDenied(View view) {
            dismiss();
        }

        public void onGranted(View view) {
            ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_CALL);
            dismiss();
        }
    }
}
