package com.hesiii.i_hear.Amplification;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import com.hesiii.i_hear.R;
import com.hesiii.i_hear.databinding.ActivitySoundAmplificationBinding;

public class SoundAmplificationActivity extends AppCompatActivity {

    private ActivitySoundAmplificationBinding binding;
    private ValueCallback<Uri> mUploadMessage;
    private ValueCallback<Uri[]> mFilePathCallback;

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

        if (getSupportActionBar() != null) {

            getSupportActionBar().hide();

        }

        try {

            binding.webView.getSettings().setJavaScriptEnabled(true);
            binding.webView.getSettings().setAllowFileAccess(true);
            binding.webView.getSettings().setAllowFileAccessFromFileURLs(true);
            binding.webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
            binding.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            binding.webView.loadUrl("http://falconvideo.lk/test/");

        } catch (Exception e) {

            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();

        }

        binding.webView.setWebChromeClient(new WebChromeClient() {
            // For Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                // Create an intent to allow the user to select a file
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                startActivityForResult(Intent.createChooser(i, "File Chooser"), 1);
                mUploadMessage = uploadMsg;
            }

            // For Android 4.1+
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                openFileChooser(uploadMsg, acceptType);
            }

            // For Android 5.0+
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                // Create an intent to allow the user to select a file
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                startActivityForResult(Intent.createChooser(i, "File Chooser"), 1);
                mFilePathCallback = filePathCallback;
                return true;
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        // Handle the file upload request result
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                Uri uri = intent.getData();
                if (mUploadMessage != null) {
                    mUploadMessage.onReceiveValue(uri);
                    mUploadMessage = null;
                } else if (mFilePathCallback != null) {
                    Uri[] results = new Uri[]{uri};
                    mFilePathCallback.onReceiveValue(results);
                    mFilePathCallback = null;
                }
            } else {
                if (mUploadMessage != null) {
                    mUploadMessage.onReceiveValue(null);
                    mUploadMessage = null;
                } else if (mFilePathCallback != null) {
                    mFilePathCallback.onReceiveValue(null);
                    mFilePathCallback = null;
                }
            }
        }
    }

}