package com.bomostory.sceneeditmodule.view

import android.content.Context
import android.graphics.drawable.Drawable
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 android.widget.ImageView
import android.widget.TextView
import com.bomostory.sceneeditmodule.basicdata.Scene
import com.bomostory.sceneeditmodule.basicdata.Story
import com.bomostory.sceneeditmodule.utils.AnimationUtils
import com.bomostory.sceneeditmodule.utils.HorizontalItemDecoration
import com.bomostory.sceneeditmodule.utils.TimeUtils
import com.example.tfat.myapplication.R
import kotlinx.android.synthetic.main.movie_item_view.view.*
import kotlinx.android.synthetic.main.movie_select_view.view.*

class MovieSelectView : ConstraintLayout {

    interface OnMovieSelectListener {
        fun onMoveSelect(scene: Scene)
    }

    var onMovieSelectListener: OnMovieSelectListener? = null

    var story: Story? = null
        set(value) {
            field = value
            movieList?.apply {
                adapter = MovieAdapter()
            }

        }

    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_select_view, this)

        movieList.apply {
            setHasFixedSize(true)
            addItemDecoration(HorizontalItemDecoration(resources.getDimensionPixelSize(R.dimen.horizontal_item_spacing)))
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        }

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

            AnimationUtils.collapse(movieList, 300, object : Animation.AnimationListener {
                override fun onAnimationEnd(animation: Animation?) {
                    movieList.visibility = View.GONE
                    movieBar.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(movieList, 300, object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                    movieList.visibility = View.VISIBLE
                    movieBar.visibility = View.GONE
                }

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

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

        override fun getItemCount(): Int {
            story?.let {
                return it.scenes.size
            }
            return 0
        }

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

        inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            fun bind(p1: Int) {
                itemView.apply {
                    story?.apply {
                        number.text = "$p1"
                        screenshot.setImageDrawable(Drawable.createFromPath(this.scenes[p1].backgroundPath))
                        this.scenes[p1].record?.let {
                            period.text = TimeUtils.getPlayMovieTimeFormat(it.period)
                        }
                        itemView.setOnClickListener {
                            onMovieSelectListener?.onMoveSelect(this.scenes[p1])
                        }
                    }
                }
            }
        }
    }

}