package com.tencent.liteav.demo.player

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.tencent.liteav.demo.player.util.ModelParser
import com.tencent.liteav.demo.player.util.ModelProvider
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getRootView())
    }

    private fun getRootView(): View {
        val linearLayout = LinearLayout(this@MainActivity).apply {
            val button = Button(this@MainActivity).apply {
                setOnClickListener {
                    var inputStream: InputStream? = null
                    var outputStream: ByteArrayOutputStream? = null

                    try {
                        assets.open("data.json").bufferedReader().use {
                            val data = it.readText()
                            ModelParser.parse(data)
                        }

                        val intent = Intent(this@MainActivity, PlayerActivity::class.java)
                        startActivity(intent)
                    } catch (e: Throwable) {
                        e.printStackTrace()
                    } finally {
                        inputStream?.close()
                        outputStream?.close()
                    }

                }

                text = "进入播放器"
                textSize = 24f

                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                ).apply {
                    gravity = Gravity.CENTER
                }
            }
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            addView(button)
        }

        return linearLayout
    }
}