package com.ytpm.hydtw.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.ytpm.hydtw.adapter.ZixunAdapter
import com.ytpm.hydtw.dao.IdiomStory
import com.ytpm.hydtw.databinding.FragmentZixuBinding
import com.ytpm.hydtw.utils.JsonReader

class ZixunFragment : Fragment() {

    private lateinit var adapter: ZixunAdapter
    private lateinit var binding: FragmentZixuBinding
    private val idiomList = mutableListOf<IdiomStory>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentZixuBinding.inflate(inflater)
        ImmersionBar.with(this)
            .transparentStatusBar()
            .statusBarDarkFont(true)
            .titleBar(binding.title)
            .init()
        initView()
        return binding.root
    }

    private fun initView() {
        adapter = ZixunAdapter(idiomList)
        binding.rv.adapter = adapter
        loadIdiomData()
    }


    private fun loadIdiomData() {
        val jsonString = context?.let { JsonReader.readJsonFromAssets(it, "gs.json") }

        jsonString?.let {
            val idiomStories = JsonReader.parseIdiomStories(it)
            idiomList.addAll(idiomStories)
        }
    }
}