package cn.i2edu.dubbing_lib

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Build
import android.view.View
import cn.i2edu.dubbing_lib.util.*
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
import io.flutter.plugin.common.PluginRegistry.Registrar

class DubbingLibPlugin : MethodCallHandler {

    private val activity: Activity
    private val pausableThreadPool: PausableThreadPool = PausableThreadPool(1)

    companion object {

        private lateinit var channel: MethodChannel
        val CHANNEL_NAME = "cn.hwwwwh.flutter.plugins/DubbingPlugin"

        @JvmStatic
        fun registerWith(registrar: Registrar) {
            if (registrar.activity() == null) return
            channel = MethodChannel(registrar.messenger(), "dubbing_lib")
            channel.setMethodCallHandler(DubbingLibPlugin(registrar.activity()))
        }
    }

    @SuppressLint("CheckResult")
    constructor(activity: Activity) {
        this.activity = activity
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        when (call.method) {
            "getPlatformVersion" -> result.success("Android ${android.os.Build.VERSION.RELEASE}")
            "setExtraFullScreen" -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility.or(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
                }
            }
            "mixinAudio" -> {
                try {
                    MixinPaintedUtil.getInstance(context = activity.applicationContext)
                            .initParams(audioPaths = call.argument<List<String>>("audioPaths")!!, bgmPath = call.argument<String>("bgmPath")!!,
                                    durationList = call.argument<List<Long>>("durationList")!!, endTimeList = call.argument<List<Long>>("endTimeList")!!,
                                    decodeDirPath = call.argument<String>("decodeDirPath")!!,
                                    mixinFilePath = call.argument<String>("mixinFilePath")!!,
                                    resultPath = call.argument<String>("resultPath")!!)
                            .setComposeCallBack(
                                    object : MixinPaintedCallBack {
                                        override fun onResult(resultPath: String) {
                                            activity.runOnUiThread {
                                                result.success(resultPath)
                                            }
                                        }

                                        override fun onError(message: String) {
                                            activity.runOnUiThread {
                                                result.error("1005", "message", null)
                                            }
                                        }
                                    }
                            )
                            .startMixin()
                } catch (e: Exception) {
                    e.printStackTrace()
                    result.error("1005", "mixin audio failed", null)
                }
            }
            "mixinVideoAndAudio" -> {
                try {
                    val resultPath = call.argument<String>("resultPath")!!
                    pausableThreadPool.execute {
                        val videoAudioMixer = Mp4ParserMixer(activity.applicationContext)
                        videoAudioMixer.setListener(object : Mp4ParserMixer.VideoAudioMixListener {
                            override fun mixSuccess() {
                                activity.runOnUiThread {
                                    result.success(resultPath)
                                }
                            }

                            override fun mixFail(reason: String?) {
                                activity.runOnUiThread {
                                    result.error("1005", "error: $reason", null)
                                }
                            }
                        })

                        videoAudioMixer.muxAudioAndVideo(
                                call.argument<String>("bgmPath")!!,
                                call.argument<String>("videoPath")!!,
                                resultPath)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    result.error("1006", "mixin video failed", null)
                }
            }
            else -> result.notImplemented()
        }
    }
}
