import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import com.ydl.ydl_router.manager.YDLRouterManager
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.course.BuildConfig
import com.yidianling.course.CourseConstants
import com.yidianling.course.courseNew.mine.MyCourseActivity
import com.yidianling.course.course_special_list.activity.CourseSpecialListActivity
import com.yidianling.course.flutterPlugin.CourseSendPlugin
import com.yidianling.course.router.CourseIn
import com.yidianling.ydlcommon.app.YdlCommonApp
import com.yidianling.ydlcommon.flutter.base.BaseFlutterFragment
import com.yidianling.ydlcommon.h5.H5Params
import com.yidianling.ydlcommon.h5.NewH5Activity
import com.yidianling.ydlcommon.player.YDLMusicHelper
import com.yidianling.ydlcommon.router.YdlCommonRouterManager
import com.yidianling.ydlcommon.utils.PlayerFloatHelper
import com.yidianling.ydlcommon.view.PlayerFloatView
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.view.FlutterView

/**
 * @author jiucheng
 * @描述:
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/10/18
 */
class CoursePlugin : MethodChannel.MethodCallHandler {
    private var mFragment: BaseFlutterFragment? = null

    private constructor(fragment: BaseFlutterFragment) {
        mFragment = fragment
    }

    companion object {
        const val CHANNEL: String = "base/channel/native/get"
        const val GETPUBLICPARAMAS: String = "getPublicParamas"
        const val JUMPURL: String = "jumpUrl"
        const val PLAYCOURSEFILE: String = "playCourseFile"
        const val PAUSECOURSEPLAY: String = "pauseCoursePlay"

        fun rigister(fragment: BaseFlutterFragment, flutterView: FlutterView) {
            MethodChannel(flutterView, CHANNEL).setMethodCallHandler(CoursePlugin(fragment))
        }
    }

    override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {
        when (methodCall.method) {
            GETPUBLICPARAMAS -> {
                val mMap = mutableMapOf<String, Any>()
                mMap["isDevelopment"] = BuildConfig.DEBUG
                mMap["uid"] = YdlCommonRouterManager.getYdlCommonRoute().getUid().toString()
                mMap["accessToken"] = YdlCommonRouterManager.getYdlCommonRoute().getAccessToken()
                        ?: ""
                mMap["isFromApp"] = "1"
                mMap["version"] = RxDeviceTool.getAppVersionName(YdlCommonApp.getApp())
                mMap["osBuild"] = RxDeviceTool.getBuildMANUFACTURER() + "," + RxDeviceTool.getBuildBrandModel() + "," + RxDeviceTool.getOsBuileVersion() + "," + RxDeviceTool.getAppVersionName(YdlCommonApp.getApp())
                result.success(mMap)
            }
            JUMPURL -> {
                var jumpUrl: String? = methodCall.arguments.toString()
                if (!TextUtils.isEmpty(jumpUrl)) {
                    when {
                        jumpUrl!!.contains("user/login") -> mFragment!!.startActivity(CourseIn.loginWayIntent(mFragment!!.activity))
                        jumpUrl.contains("course/myCourse") -> MyCourseActivity.start(mFragment!!.activity)
                        jumpUrl.contains("course/specialList") -> //跳转专题列表页
                            mFragment!!.activity.startActivity(Intent(mFragment!!.activity, CourseSpecialListActivity::class.java))
                        jumpUrl.contains("course/detail") -> {
                            var uri = Uri.parse(jumpUrl)
                            var id = uri.getQueryParameter("id")
                            val h5Params = H5Params(CourseConstants.COURSE_DETAIL_H5 + id, "课程详情")
                            NewH5Activity.start(mFragment!!.activity, h5Params)
                        }
                        else -> YDLRouterManager.router(jumpUrl)
                    }
                }
            }
            PLAYCOURSEFILE -> {
                var fileInfo: Map<*, *> = methodCall.arguments as Map<*, *>
                LogUtil.e("播放地址：$fileInfo")
                playMedia(fileInfo)
            }
            PAUSECOURSEPLAY -> {
                if (YDLMusicHelper.isPlaying()) {
                    YDLMusicHelper.pause()
                    PlayerFloatHelper.updatePlayState()
                }
            }
        }
    }

    private fun playMedia(fileInfo: Map<*, *>) {
        if (PlayerFloatHelper.isShow(mFragment!!.activity)) {
            PlayerFloatHelper.hide()
            PlayerFloatHelper.removeView(mFragment!!.activity)
            YDLMusicHelper.clearTempData()
            YDLMusicHelper.stop()
        } else {
            PlayerFloatHelper.removeView(mFragment!!.activity)
        }


        YDLMusicHelper.title = fileInfo["title"].toString()
        YDLMusicHelper.name = fileInfo["name"].toString()
        YDLMusicHelper.cover = fileInfo["cover"].toString()
        YDLMusicHelper.course_id = fileInfo["courseId"] as Int
        YDLMusicHelper.isCanClick = false

        var demoType = fileInfo["demoType"] as Int
        var url = fileInfo["url"].toString()

        if (demoType == 1) {
            YDLMusicHelper.playType = 0
            PlayerFloatHelper.show(mFragment!!.activity)

            YDLMusicHelper.coursePlay(url)
        }
        if (demoType == 2) {
            YDLMusicHelper.playType = 1
            YDLMusicHelper.courseVideoUlr = url

            PlayerFloatHelper.show(mFragment!!.activity)
        }


        PlayerFloatHelper.addClickListener(object : PlayerFloatView.FloatViewPlayListener {
            override fun onStartClick() {
                if (!YDLMusicHelper.isCanClick && YDLMusicHelper.course_id != 0) {
                    CourseSendPlugin.sendMsg(true)
                }
            }

            override fun onPlayFinish() {
                PlayerFloatHelper.hide()
                YDLMusicHelper.clearTempData()
                YDLMusicHelper.stop()
            }

            override fun onPauseClick() {
                if (!YDLMusicHelper.isCanClick && YDLMusicHelper.course_id != 0) {
                    CourseSendPlugin.sendMsg(false)
                }
            }
        })
    }
}