package com.bomostory.sceneeditmodule.view

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import com.bomostory.sceneeditmodule.basicdata.Music
import com.bomostory.sceneeditmodule.utils.AnimationUtils
import com.example.audiotrack.AudioTrackView
import com.example.audiotrack.LoopAudioTrackView
import com.example.tfat.myapplication.R
import kotlinx.android.synthetic.main.audio_track_group_view.view.*
import kotlinx.android.synthetic.main.audio_track_item_view.view.*


class AudioTrackGroupView : ConstraintLayout {

    interface OnAudioTrackSelectListener {
        fun onAudioTrackSelect(position: Int)
    }

    interface OnAudioTrackScrollListener {
        fun onAudioTrackScroll(position: Int, scrollRatio: Double)
    }

    var scenePeriod: Long = 0

    var musics = ArrayList<Music>()
        set(value) {
            field = value
            audioTrackList.adapter?.notifyDataSetChanged()
        }

    var isLoop = false
        set(value) {
            field = value
            audioTrackList.adapter?.notifyDataSetChanged()
        }

    var currentPosition = 0
        set(value) {
            field = value
            audioTrackList.adapter?.notifyDataSetChanged()
        }

    var onAudioTrackSelectListener: OnAudioTrackSelectListener? = null

    var onAudioTrackScrollListener: OnAudioTrackScrollListener? = null

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        initView()
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.audio_track_group_view, this)

        audioTrackList.apply {
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = AudioTackAdapter()
        }

        expandLess.setOnClickListener {
            it.visibility = View.INVISIBLE
            expandMore.visibility = View.VISIBLE

            AnimationUtils.collapse(audioTrackList, 300, object : Animation.AnimationListener {
                override fun onAnimationEnd(animation: Animation?) {
                    audioTrackList.visibility = View.GONE
                    audioBar.visibility = View.VISIBLE
                }

                override fun onAnimationRepeat(animation: Animation?) {}
                override fun onAnimationStart(animation: Animation?) {}
            })
        }

        expandMore.setOnClickListener {
            it.visibility = View.INVISIBLE
            expandLess.visibility = View.VISIBLE

            AnimationUtils.expand(audioTrackList, 300, object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                    audioTrackList.visibility = View.VISIBLE
                    audioBar.visibility = View.GONE
                }

                override fun onAnimationRepeat(animation: Animation?) {}
                override fun onAnimationEnd(animation: Animation?) {}
            })
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var newHeightMeasureSpec = heightMeasureSpec
        if (maxHeight > 0) {
            val heightSize = View.MeasureSpec.getSize(heightMeasureSpec)
            val heightMode = View.MeasureSpec.getMode(heightMeasureSpec)

            when (heightMode) {
                View.MeasureSpec.AT_MOST -> newHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(Math.min(heightSize, maxHeight), View.MeasureSpec.AT_MOST)
                View.MeasureSpec.UNSPECIFIED -> newHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxHeight, View.MeasureSpec.AT_MOST)
                View.MeasureSpec.EXACTLY -> newHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(Math.min(heightSize, maxHeight), View.MeasureSpec.EXACTLY)
            }
        }
        super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
    }

    inner class AudioTackAdapter : RecyclerView.Adapter<AudioTackAdapter.ViewHolder>() {
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
            val view = LayoutInflater.from(p0.context).inflate(R.layout.audio_track_item_view, p0, false)
            return ViewHolder(view)
        }

        override fun getItemCount(): Int {
            return musics.size
        }

        override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
            p0.bind(p1)
        }

        inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            fun bind(p1: Int) {
                itemView.apply {
                    loopAudioTrackView.innerWidthRatio =
                            if (musics[p1].duration >= scenePeriod) 1f
                            else (musics[p1].duration).toFloat() / scenePeriod.toFloat()

                    musics[p1].name?.let {
                        loopAudioTrackView.innerText = it
                    }
                    
                    loopAudioTrackView.isLoop = isLoop
                    loopAudioTrackView.isSelect = (p1 == currentPosition)
                    loopAudioTrackView.onSelectListener = object : LoopAudioTrackView.OnSelectListener {
                        override fun onSelect() {
                            onAudioTrackSelectListener?.onAudioTrackSelect(p1)
                        }
                    }
                    loopAudioTrackView.onScrollListener = object : AudioTrackView.OnScrollListener {
                        override fun onScroll(scrollRatio: Double) {
                            onAudioTrackScrollListener?.onAudioTrackScroll(p1, scrollRatio)
                        }
                    }
                }
            }
        }
    }
}