package com.ray650128.gstreamer_demo_app.ui.mainScreen

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import androidx.viewpager2.widget.ViewPager2
import com.ray650128.gstreamer_demo_app.Constants
import com.ray650128.gstreamer_demo_app.R
import com.ray650128.gstreamer_demo_app.databinding.ActivityMainBinding
import com.ray650128.gstreamer_demo_app.model.Device
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * MainActivity.kt
 * 應用程式主畫面
 */
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val viewModel: MainViewModel by viewModels()

    private val mContext: Context by lazy { this }

    private var splitMode = 1

    private var videos: List<List<Device>>? = null

    private var videoPageList: ArrayList<SplitViewFragment> = arrayListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initContentView()

        initObservers()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    private fun initContentView() = binding.apply {
        //region Content area
        button.setOnClickListener {
            if (splitMode == Constants.SPLIT_MODE_SINGLE) return@setOnClickListener
            viewModel.setSplitMode(Constants.SPLIT_MODE_SINGLE)
            Log.e(TAG, "+++ split style: 1")
        }

        button2.setOnClickListener {
            if (splitMode == Constants.SPLIT_MODE_FOUR) return@setOnClickListener
            viewModel.setSplitMode(Constants.SPLIT_MODE_FOUR)
            Log.e(TAG, "+++ split style: 4")
        }

        button3.setOnClickListener {
            if (splitMode == Constants.SPLIT_MODE_NINE) return@setOnClickListener
            viewModel.setSplitMode(Constants.SPLIT_MODE_NINE)
            Log.e(TAG, "+++ split style: 9")
        }

        button4.setOnClickListener {
            if (splitMode == Constants.SPLIT_MODE_SIXTEEN) return@setOnClickListener
            viewModel.setSplitMode(Constants.SPLIT_MODE_SIXTEEN)
            Log.e(TAG, "+++ split style: 16")
        }
        //endregion
    }

    private fun initObservers() {
        viewModel.splitMode.observe(this) {
            splitMode = it
            Log.e(TAG, "Split count: $it")
        }

        viewModel.cameraList.observe(this) { list ->
            this.videos = list
            reloadVideoViews(this.videos)
        }
    }

    private fun reloadVideoViews(list: List<List<Device>>?) {
        for (videoPage in videoPageList) {
            supportFragmentManager.commit {
                remove(videoPage)
            }
        }
        binding.viewPager.removeAllViews()
        videoPageList.clear()

        // 如果群組內沒有裝置，則顯示底圖
        if (list.isNullOrEmpty()) {
            binding.viewPager.setBackgroundResource(R.drawable.bg_not_in_playing)
        } else {
            binding.viewPager.setBackgroundColor(Color.TRANSPARENT)
            for (i in list.indices) {
                val splitFragment = SplitViewFragment.newInstance(
                    pageNumber = i,
                    splitMode = splitMode,
                    pageData = ArrayList(list[i])
                )
                videoPageList.add(i, splitFragment)

                val frameLayout = FrameLayout(mContext).apply {
                    id = View.generateViewId()
                }
                binding.viewPager.addView(frameLayout)
                supportFragmentManager.commit {
                    add(frameLayout.id, splitFragment, "$i")
                }
            }
        }
    }


    companion object {
        private val TAG = MainActivity::class.java.simpleName
    }
}