import Flutter
import UIKit

public class VapFlutterView: NSObject, FlutterPlatformView {
    private let containerView: UIView
    private let channel: FlutterMethodChannel
    private var vapTagContents: [String: [String: Any]] = [:]
    private var isMuted = false
    private var repeatCount = 0
    private var scaleType = "fitCenter"

    init(
        context: CGRect,
        params: [String: Any]?,
        messenger: FlutterBinaryMessenger,
        id: Int64
    ) {
        containerView = UIView(frame: context)
        channel = FlutterMethodChannel(name: "vap_view_\(id)", binaryMessenger: messenger)
        super.init()

        containerView.backgroundColor = .clear
        channel.setMethodCallHandler(onMethodCall)

        if let params {
            if let initialScaleType = params["scaleType"] as? String {
                scaleType = initialScaleType
            }
            if let initialLoop = params["loop"] as? Int {
                repeatCount = initialLoop
            }
            if let initialMute = params["mute"] as? Bool {
                isMuted = initialMute
            }
            if let tagContents = params["vapTagContents"] as? [String: [String: Any]] {
                vapTagContents = tagContents
            }
        }
    }

    public func view() -> UIView {
        containerView
    }

    private func onMethodCall(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "dispose":
            channel.setMethodCallHandler(nil)
            result(nil)
        case "playFile", "playAsset":
            notifyUnsupportedPlayback(call.method)
            result(nil)
        case "stop":
            result(nil)
        case "setLoop":
            if let args = call.arguments as? [String: Any], let loop = args["loop"] as? Int {
                repeatCount = loop
            }
            result(nil)
        case "setMute":
            if let args = call.arguments as? [String: Any], let mute = args["mute"] as? Bool {
                isMuted = mute
            }
            result(nil)
        case "setScaleType":
            if let args = call.arguments as? [String: Any], let value = args["scaleType"] as? String {
                scaleType = value
            }
            result(nil)
        case "setVapTagContent":
            if let args = call.arguments as? [String: Any],
               let tag = args["tag"] as? String,
               let content = args["content"] as? [String: Any] {
                vapTagContents[tag] = content
            }
            result(nil)
        case "setVapTagContents":
            if let args = call.arguments as? [String: Any],
               let contents = args["contents"] as? [String: [String: Any]] {
                vapTagContents = contents
            }
            result(nil)
        case "getVapTagContent":
            if let args = call.arguments as? [String: Any], let tag = args["tag"] as? String {
                result(vapTagContents[tag])
            } else {
                result(nil)
            }
        case "getAllVapTagContents":
            result(vapTagContents)
        case "clearVapTagContents":
            vapTagContents.removeAll()
            result(nil)
        default:
            result(FlutterMethodNotImplemented)
        }
    }

    private func notifyUnsupportedPlayback(_ method: String) {
        channel.invokeMethod("onFailed", arguments: [
            "errorCode": "-1",
            "type": "unsupported",
            "msg": "QGVAPlayer is disabled for this iOS build. \(method) is unavailable."
        ])
    }
}
