package com.doverfuelingsolutions.issp.view

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.data.DataStore
import com.doverfuelingsolutions.issp.databinding.ActivityMainBinding
import com.doverfuelingsolutions.issp.fusion.FusionManager
import com.doverfuelingsolutions.issp.utils.ActivityUtil
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import kotlinx.coroutines.*
import java.io.File

class MainActivity : AppCompatActivity(), View.OnClickListener {

    companion object {

        fun start(context: Context) {
            context.startActivity(Intent(context, MainActivity::class.java))
        }
    }

    private lateinit var activityMainBinding: ActivityMainBinding
    private val mainViewModel = MainViewModel()

    private val coroutineScope = CoroutineScope(Dispatchers.Main)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        activityMainBinding.lifecycleOwner = this
        activityMainBinding.mainViewModel = mainViewModel

        activityMainBinding.stationLogo.setImageURI(Uri.fromFile(File(filesDir, DataStore.stationLogoFile)))
        mainViewModel.stationName.value = DataStore.buName

        initBanner()

        lifecycle.addObserver(FusionManager)

        activityMainBinding.textClock.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        ActivityUtil.setFullscreen(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            activityMainBinding.textClock -> SettingsActivity.start(this)
        }
    }

    private fun initBanner() {
        activityMainBinding.banner.run {
            addBannerLifecycleObserver(this@MainActivity)
            scrollTime = 500
            adapter = object : BannerImageAdapter<Int>(listOf(R.mipmap.banner_1, R.mipmap.banner_2)) {
                override fun onBindView(holder: BannerImageHolder, data: Int, position: Int, size: Int) {
                    holder.imageView.setImageResource(data)
                }
            }
        }
    }

    class MainViewModel : ViewModel() {
        val stationName = MutableLiveData<String>()
    }
}