package chaoran.business.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import chaoran.business.R;
import chaoran.business.utils.FileChooserHelper;
import chaoran.business.utils.CameraHelper;

/**
 * 文件选择测试Activity
 */
public class FileTestActivity extends AppCompatActivity {

    private WebView webView;
    private FileChooserHelper fileChooserHelper;
    private CameraHelper cameraHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_test);

        fileChooserHelper = new FileChooserHelper(this);
        
        webView = findViewById(R.id.webViewFileTest);
        cameraHelper = new CameraHelper(this, webView);
        initWebView();
        
        // 加载测试页面
        webView.loadUrl("file:///android_asset/demo/file_test.html");
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        
        // 添加JavaScript接口
        webView.addJavascriptInterface(cameraHelper, "CameraHelper");
        
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
                                             FileChooserParams fileChooserParams) {
                // 检查是否接受图片
                String[] acceptTypes = fileChooserParams.getAcceptTypes();
                boolean acceptImage = false;
                boolean acceptCamera = false;
                boolean cameraOnly = false;

                if (acceptTypes != null && acceptTypes.length > 0) {
                    for (String type : acceptTypes) {
                        if (type.contains("image")) {
                            acceptImage = true;
                            break;
                        }
                    }
                }

                // 检查capture模式
                if (fileChooserParams.isCaptureEnabled()) {
                    acceptCamera = true;
                    
                    // 注意：Android WebView中，capture="user"或capture="environment"都会返回true
                    // 我们通过简单的方式判断：如果是图片且启用capture，默认显示选择器
                    // 只有在特定情况下才直接打开相机
                    
                    // 这里我们不设置cameraOnly=true，让所有capture都显示选择器
                    // 如果需要仅相机模式，可以通过其他方式（如URL参数）来控制
                }

                fileChooserHelper.openFileChooser(filePathCallback, acceptImage, acceptCamera, cameraOnly);
                return true;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (!fileChooserHelper.onActivityResult(requestCode, resultCode, data)) {
            cameraHelper.onActivityResult(requestCode, resultCode, data);
        }
    }

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

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