package com.kaviz.heal.Activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.kaviz.heal.R;
import com.kaviz.heal.databinding.ActivityChatBotBinding;

public class ChatBotActivity extends AppCompatActivity {

    private ActivityChatBotBinding binding;
    private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 101;
    private PermissionRequest myRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChatBotBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (getSupportActionBar() != null) {

            getSupportActionBar().setTitle(getResources().getString(R.string.live_chat));

        }

        setWebView();

//        try {
//
//            binding.chatBotWebView.getSettings().setJavaScriptEnabled(true);
//            binding.chatBotWebView.getSettings().setUseWideViewPort(true);
//            binding.chatBotWebView.getSettings().setLoadWithOverviewMode(true);
//            binding.chatBotWebView.loadUrl("https://kosalaproject.000webhostapp.com/");
//
//        } catch (Exception e) {
//
//            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
//
//        }

    }

    private void setWebView() {
        binding.chatBotWebView.getSettings().setJavaScriptEnabled(true);
        binding.chatBotWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        binding.chatBotWebView.setWebViewClient(new WebViewClient());

        binding.chatBotWebView.getSettings().setSaveFormData(true);
        binding.chatBotWebView.getSettings().setSupportZoom(false);
        binding.chatBotWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        binding.chatBotWebView.getSettings().setPluginState(WebSettings.PluginState.ON);


        binding.chatBotWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                myRequest = request;

                for (String permission : request.getResources()) {
                    switch (permission) {
                        case "android.webkit.resource.AUDIO_CAPTURE": {
                            askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
                            break;
                        }
                    }
                }
            }
        });

        binding.chatBotWebView.loadUrl("https://kosalaproject.000webhostapp.com/");
    }

    @Override
    public void onBackPressed() {
        if (binding.chatBotWebView.canGoBack()) {
            binding.chatBotWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_RECORD_AUDIO: {
                Log.d("WebView", "PERMISSION FOR AUDIO");
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    myRequest.grant(myRequest.getResources());
                    binding.chatBotWebView.loadUrl("https://kosalaproject.000webhostapp.com/");

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    public void askForPermission(String origin, String permission, int requestCode) {
        Log.d("WebView", "inside askForPermission for" + origin + "with" + permission);

        if (ContextCompat.checkSelfPermission(getApplicationContext(),
                permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(ChatBotActivity.this,
                    permission)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(ChatBotActivity.this,
                        new String[]{permission},
                        requestCode);
            }
        } else {
            myRequest.grant(myRequest.getResources());
        }
    }

}