package com.bomostory.sceneeditmodule

import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bomostory.sceneeditmodule.basicdata.MusicSource
import com.example.tfat.myapplication.R
import com.google.gson.Gson
import kotlinx.android.synthetic.main.dialog_music_select.view.*

private const val ARG_MUSIC_SOURCE = "music_source"

class MusicSelectDialog : DialogFragment() {
    companion object {
        @JvmStatic
        fun newInstance(musicSource: String) =
                MusicSelectDialog().apply {
                    arguments = Bundle().apply {
                        putString(ARG_MUSIC_SOURCE, musicSource)
                    }
                }
    }

    private var musicSource: MusicSource? = null
    private var listener: DialogInterface.OnDismissListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        musicSource = Gson().fromJson<MusicSource>(arguments?.getString(ARG_MUSIC_SOURCE), MusicSource::class.java)

        return inflater.inflate(R.layout.dialog_music_select, container).apply {
            val simpleFragmentPagerAdapter = SimpleFragmentPagerAdapter(childFragmentManager)

            viewPager.adapter = simpleFragmentPagerAdapter
            tabLayout.setupWithViewPager(viewPager)
        }
    }

    override fun onDismiss(dialog: DialogInterface?) {
        super.onDismiss(dialog)
        listener?.onDismiss(dialog)
    }

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

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

    inner class SimpleFragmentPagerAdapter(fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager) {
        override fun getItem(p0: Int): Fragment {
            when (p0) {
                0 -> {
                    return MusicSelectFragment.newInstance(Gson().toJson(musicSource?.library))
                }
                1 -> {
                    return MusicSelectFragment.newInstance(Gson().toJson(musicSource?.device))
                }
            }
            return MusicSelectFragment()
        }

        override fun getCount(): Int {
            return 2
        }

        override fun getPageTitle(position: Int): CharSequence? {
            when (position) {
                0 -> return "Library"
                1 -> return "Device"
            }
            return super.getPageTitle(position)
        }
    }
}