package com.shuangyubang.mofun_flutter_plugin_video

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory


const val Channel_Mofun_Video_Info = "plugins.shuangyunbang.mofun/mofunvideoview"

class MofunVideoView
    : PlatformView, MethodChannel.MethodCallHandler {

    private val textView: TextView

    constructor(context: Context, messenger: BinaryMessenger, id: Int, creationParams: Any?) {
        textView = TextView(context)
        textView.setBackgroundColor(Color.BLUE)
        MethodChannel(messenger, Channel_Mofun_Video_Info + "_$id").setMethodCallHandler(this)
    }

    override fun getView(): View {
        return textView
    }

    override fun dispose() {

    }

    override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {
        when (methodCall.method) {
            "setText" -> setText(methodCall, result)
            else -> result.notImplemented()
        }
    }

    private fun setText(methodCall: MethodCall, result: MethodChannel.Result) {
        val text = methodCall.arguments as String
        textView.text = text
        result.success(null)
    }

}

class MofunVideoViewFactory(val messenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {

    override fun create(context: Context, id: Int, creationParams: Any?): PlatformView {
        return MofunVideoView(context, messenger, id, creationParams)
    }

}