package com.bomostory.sceneeditmodule

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bomostory.sceneeditmodule.basicdata.Music
import com.bomostory.sceneeditmodule.utils.TimeUtils

import com.example.tfat.myapplication.R
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.music_item_view.view.*

private const val ARG_MUSICS = "musics"

/**
 * A simple [Fragment] subclass.
 * Activities that contain this fragment must implement the
 * [MusicSelectFragment.OnFragmentInteractionListener] interface
 * to handle interaction events.
 * Use the [MusicSelectFragment.newInstance] factory method to
 * create an instance of this fragment.
 *
 */
class MusicSelectFragment : Fragment() {
    companion object {
        @JvmStatic
        fun newInstance(musics: String) =
                MusicSelectFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_MUSICS, musics)
                    }
                }
    }

    interface OnFragmentInteractionListener {

        fun onMusicPlayClick(path: String)

        fun onMusicPauseClick()

        fun onMusicImportClick(music: Music)
    }

    private var listener: OnFragmentInteractionListener? = null
    private var musics: ArrayList<Music> = ArrayList()
    private var playPosition: Int = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            musics = Gson().fromJson(it.getString(ARG_MUSICS), object : TypeToken<ArrayList<Music>>() {}.type)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_music_select, container, false).apply {
            val musicList = findViewById<RecyclerView>(R.id.musicList)

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

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnFragmentInteractionListener) {
            listener = context
        } else {
            throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }

    inner class MusicAdapter : RecyclerView.Adapter<MusicAdapter.ViewHolder>() {
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
            val view = LayoutInflater.from(p0.context).inflate(R.layout.music_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 {
                    title.text = musics[p1].name
                    duration.text = TimeUtils.getPlayMovieTimeFormat(musics[p1].duration)

                    play.visibility = if (p1 != playPosition) View.VISIBLE else View.INVISIBLE
                    pause.visibility = if (p1 == playPosition) View.VISIBLE else View.INVISIBLE

                    play.setOnClickListener {
                        musics[p1].path?.let {
                            listener?.onMusicPlayClick(it)
                        }

                        playPosition = p1
                        notifyDataSetChanged()
                    }

                    pause.setOnClickListener {
                        listener?.onMusicPauseClick()

                        playPosition = -1
                        notifyDataSetChanged()
                    }

                    importMusic.setOnClickListener {
                        listener?.onMusicImportClick(musics[p1])
                    }
                }
            }
        }
    }
}
