package com.duolebo.blyrobot.data

import com.duolebo.appbase.IModel
import com.duolebo.appbase.prj.XMLHelper
import org.json.JSONArray
import org.json.JSONObject

class ApkInfo: IModel {
    lateinit var name: String
    //包名
    lateinit var packageName: String
    //启动apk配置信息
    lateinit var launcher: String
    //截图存放路径
    lateinit var imagePath: String
    // 过滤媒体视频扩展名信息
    lateinit var mediaExt: String
    // 播放地址过滤url
    lateinit var filterUrl: String
    // 抓取频道数量
    var channelCount = 5
    // 启动延时(秒)
    var launchDelay = 15
    // 频道抓取延时(秒)
    var captureDelay = 15
    //启动后按键事件组合
    var launchKeyEvent: String? = null
    // 切换频道按键事件组合
    lateinit var channelKeyEvent: String
    // 弹出界面菜单按键，默认确定键
    lateinit var menuKeyEvent: String

    // epg是否需要按返回键，因为有些apk的epg显示时间过长
    var epgBack = false

    var monitoring = true

    override fun from(json: JSONObject): Boolean {
        this.name = json.optString(NAME)
        this.packageName = json.optString(PACKAGE_NAME)
        this.launcher = json.optString(LAUNCHER)
        this.launchDelay = json.optInt(LAUNCH_DELAY, 15)
        this.captureDelay = json.optInt(CAPTURE_DELAY, 15)
        this.launchKeyEvent = json.optString(LAUNCH_KEY_EVENT, "")
        this.channelCount = json.optInt(CHANNEL_COUNT, 5)
        this.channelKeyEvent = json.optString(CHANNEL_KEY_EVENT, "down")
        this.mediaExt = json.optString(MEDIA_EXT, VIDEO_EXTS)
        this.filterUrl = json.optString(FILTER_URL, "")
        this.monitoring = json.optBoolean(MONITORING, true)

        var menuKeyEvent = json.optString(MENU_KEY_EVENT)
        // 如果包含百分号则epg需要按返回键
        if (!menuKeyEvent.isNullOrEmpty() && menuKeyEvent.contains('%')) {
            this.epgBack = true
            menuKeyEvent = menuKeyEvent.replace("%", "")
        }
        this.menuKeyEvent = menuKeyEvent

        return true
    }

    override fun from(p0: JSONArray?): Boolean {
        return false
    }

    override fun from(p0: XMLHelper?): Boolean {
        return false
    }

    companion object {
        const val VIDEO_EXTS = "m3u8|.ts|.mp4|.rmvb|.mkv|.wmv"

        //data key
        const val NAME = "name"
        const val PACKAGE_NAME = "packageName"
        const val LAUNCHER = "launcher"
        const val LAUNCH_DELAY = "launchDelay"
        const val CAPTURE_DELAY = "captureDelay"
        const val LAUNCH_KEY_EVENT = "launchKeyEvent"
        const val CHANNEL_KEY_EVENT = "channelKeyEvent"
        const val CHANNEL_COUNT = "channelCount"
        const val MEDIA_EXT = "mediaExt"
        const val FILTER_URL = "filterUrl"
        const val MENU_KEY_EVENT = "menuKeyEvent"
        const val MONITORING = "monitoring"
    }

}