package org.pixeldroid.app.settings

import android.os.Bundle
import com.google.gson.Gson
import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.OpenSourceLicenseBinding
import org.pixeldroid.app.settings.licenseObjects.Libraries
import org.pixeldroid.app.settings.licenseObjects.OpenSourceItem
import org.pixeldroid.app.utils.BaseThemedWithBarActivity

/**
 * Displays licenses for all app dependencies. JSON is
 * generated by the plugin https://github.com/cookpad/LicenseToolsPlugin.
 */
class LicenseActivity: BaseThemedWithBarActivity() {

    private lateinit var binding: OpenSourceLicenseBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = OpenSourceLicenseBinding.inflate(layoutInflater)

        setContentView(binding.root)

        supportActionBar?.setTitle(R.string.dependencies_licenses)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setHomeButtonEnabled(true)

        setupRecyclerView()
    }

    private fun setupRecyclerView() {
        val text: String = applicationContext.assets.open("licenses.json")
            .bufferedReader().use { it.readText() }

        val listObj: List<OpenSourceItem> = Gson().fromJson(text, Libraries::class.java).libraries

        val adapter = OpenSourceLicenseAdapter(listObj)
        binding.openSourceLicenseRecyclerView.adapter = adapter
    }
}