package com.atmob.system_share;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.io.File;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * SystemSharePlugin
 */
public class SystemSharePlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;

    public static final String METHOD_IS_INSTALLED = "isInstalled";
    public static final String METHOD_SHARE_FILE = "shareFile";

    private Context applicationContext;

    private static final String TAG = "SystemSharePlugin";


    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        applicationContext = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "system_share");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        switch (call.method) {
            case METHOD_IS_INSTALLED:
                isInstalled(call, result);
                break;
            case METHOD_SHARE_FILE:
                shareFile(call, result);
                break;
            default:
                result.notImplemented();
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    private void isInstalled(MethodCall call, Result result) {
        String packageName = call.argument("packageName");
        if (TextUtils.isEmpty(packageName)) {
            result.error("-1", "SystemShare: packageName is empty or null", null);
            return;
        }
        boolean installed = SystemShareUtil.isInstalled(applicationContext, packageName);
        result.success(installed);
    }

    private void shareFile(MethodCall call, Result result) {
        try {
            String filePath = call.argument("filePath");
            String packageName = call.argument("packageName");
            String shareTitle = call.argument("shareTitle");
            String shareFileType = call.argument("shareFileType");
            if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(packageName)) {
                result.error("-1", "SystemShare: filePath or packageName is empty or null", null);
                return;
            }
            File file = new File(filePath);
            if (!file.exists()) {
                result.error("-1", "SystemShare: file not exists", null);
                return;
            }
            if (!SystemShareUtil.isInstalled(applicationContext, packageName)) {
                result.error("-1", "SystemShare: app not installed", null);
                return;
            }
            SystemShareUtil.shareFile(applicationContext, file, packageName, shareTitle,shareFileType);
            result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            result.error("-1", "SystemShare: share file failed", null);
        }
    }
}
