package melody.offline.music.view

import android.annotation.SuppressLint
import android.app.Activity
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.json.JSONObject
import melody.offline.music.R
import melody.offline.music.adapter.TowRowListAdapter
import melody.offline.music.innertube.Innertube
import melody.offline.music.util.AnalysisUtil

@SuppressLint("ViewConstructor")
class MusicTowRowListView(context: Activity, homePage: Innertube.HomePage) : ModuleView(context) {
    init {
        contentView = inflate(getContext(), R.layout.music_list_layout, this)
        val title = contentView?.findViewById<TextView>(R.id.title)
        title?.text = homePage.title

        val rv = contentView?.findViewById<RecyclerView>(R.id.rv)

        val adapter = TowRowListAdapter(context, homePage.contents)
        adapter.setOnItemClickListener(object : TowRowListAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                val jsonObject = JSONObject()
                jsonObject.put("moduleName", homePage.title)
                val map = mutableMapOf(Pair(AnalysisUtil.PARAM_VALUE, jsonObject.toString()))
                AnalysisUtil.logEvent(AnalysisUtil.HOME_B_MODULE_CLICK, map)
            }
        })
        rv?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        rv?.adapter = adapter
    }

}