package com.bomostory.sceneeditmodule.view

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.SeekBar
import com.bomostory.sceneeditmodule.basicdata.Scene
import com.bomostory.sceneeditmodule.utils.TimeUtils
import com.example.tfat.myapplication.R
import kotlinx.android.synthetic.main.movie_seek_view.view.*

class MovieSeekView : ConstraintLayout {

    var scene: Scene? = null
        set(value) {
            field = value
            value?.let {
                it.record?.let {
                    progressTime.text = TimeUtils.getPlayMovieTimeFormat(playTime)
                    totalTime.text = TimeUtils.getPlayMovieTimeFormat(it.period)
                    seekBar.progress = playTime.toInt()
                    seekBar.max = it.period.toInt()
                }
            }
        }

    var playTime: Long = 0
        set(value) {
            progressTime.text = TimeUtils.getPlayMovieTimeFormat(value)
            seekBar.progress = value.toInt()

            if (seekBar.max == value.toInt()) {
                play.visibility = View.VISIBLE
                pause.visibility = View.INVISIBLE
            }
        }

    var onPlayClickListener: OnClickListener? = null
        set(value) {
            play.setOnClickListener {
                value?.onClick(it)
                play.visibility = View.INVISIBLE
                pause.visibility = View.VISIBLE
            }
        }

    var onPauseClickListener: OnClickListener? = null
        set(value) {
            pause.setOnClickListener {
                value?.onClick(it)
                play.visibility = View.VISIBLE
                pause.visibility = View.INVISIBLE
            }
        }

    var onSeekBarChangeListener: SeekBar.OnSeekBarChangeListener? = null
        set(value) {
            seekBar.setOnSeekBarChangeListener(value)
        }

    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.movie_seek_view, this)
    }
}