package com.yidianling.home.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.media.MediaPlayer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydlcommon.utils.Utils
import com.yidianling.home.MeditationViewPagerAdapter
import com.yidianling.home.R
import com.yidianling.home.constract.HomeViewConfig
import com.yidianling.home.event.IHomeEvent
import com.yidianling.home.model.bean.MeditationModuleBean
import kotlinx.android.synthetic.ydl.home_muse_view.view.*

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 冥想模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/09/10
 */
class HomeMuseView(private val mContext: Context, private var homeEvent: IHomeEvent?) :
    LinearLayout(mContext) {

    private val meditationTitles = mutableListOf<String>()

    init {
        initView()
    }

    @SuppressLint("RestrictedApi")
    private fun initView() {
        orientation = VERTICAL
        val params = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        View.inflate(mContext, R.layout.home_muse_view, this)
        homeModuleMuseViewHomeCommonTitleView.setTitle(HomeViewConfig.getOrder().museTitle)
    }

    fun initData(bean: MeditationModuleBean?) {

        if (null != bean) {

            val moreLink = bean.mditationIndexInfo?.upperFloorMore

            homeModuleMuseViewHomeCommonTitleView.setOnClickListener {
//                homeEvent?.museMoreClick(Gson().toJson(bean))

                if (Utils.isFastClick()) {
                    //防止连击
                    return@setOnClickListener
                }
                if (!moreLink.isNullOrBlank()) {
                    NewH5Activity.start(context, H5Params(moreLink, null))
                }

            }

            val greeting = bean.mditationIndexInfo?.greetings?:"你好"
            tv_time.text = greeting
            val slogan =
                if (bean.mditationIndexInfo?.desc.isNullOrBlank()){
                    "无常的日子里，呼吸间拾起力量"
                }else{
                    bean?.mditationIndexInfo?.desc
                }
            tv_meditation_slogan.text = slogan

            val videoUrl = bean?.mditationIndexInfo?.videoLink

            if (videoUrl.isNullOrBlank()){
                Glide.with(this)
                    .load(bean.mditationIndexInfo?.mditationIndexUrl)
                    .into(iv_video_background)
            }else{
                GlideApp.with(this)
                    .load(bean.mditationIndexInfo?.mditationIndexUrl)
                    .into(iv_video_background)

                video_view?.setVideoPath(videoUrl)

                video_view?.setOnErrorListener { mp, what, extra ->
                    Glide.with(this)
                        .load(bean.mditationIndexInfo?.mditationIndexUrl)
                        .into(iv_video_background)
                    true
                }
                video_view?.setOnPreparedListener {
                    it?.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
                    video_view?.setOnInfoListener { mp, what, extra ->
                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START){
                            iv_video_background.visibility = View.GONE
                            video_view.setBackgroundColor(Color.TRANSPARENT)
                        }
                        true
                    }
                    it?.start()
                    it?.setVolume(0.0F,0.0F)
                }
                video_view?.setOnCompletionListener { video_view?.start() }
            }


            bean?.mditationListResponse?.forEach {
                meditationTitles.add(it.comment)
            }

            view_pager.adapter = MeditationViewPagerAdapter(mContext,bean)

            tab_layout.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    var text:String? = tab?.text.toString().trim() ?: return
                    val spStr = SpannableString(text)
                    val styleSpan = StyleSpan(Typeface.BOLD)
                    spStr.setSpan(styleSpan,0,text?.length?:0,Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
                    tab?.text = spStr
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    var text:String? = tab?.text.toString().trim() ?: return
                    val spStr = SpannableString(text)
                    val styleSpan = StyleSpan(Typeface.NORMAL)
                    spStr.setSpan(styleSpan,0,text?.length?:0,Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
                    tab?.text = spStr
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })

            TabLayoutMediator(tab_layout,view_pager){
                tab, position ->
                    tab.text = meditationTitles[position]
            }.attach()

        }
    }

}