package com.doverfuelingsolutions.issp.view

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.data.GlobalData
import com.doverfuelingsolutions.issp.databinding.ActivityMainBinding
import com.doverfuelingsolutions.issp.fusion.FusionError
import com.doverfuelingsolutions.issp.fusion.FusionManager
import com.doverfuelingsolutions.issp.fusion.callback.OnFusionEvent
import com.doverfuelingsolutions.issp.utils.ActivityUtil
import com.doverfuelingsolutions.issp.utils.StringUtil
import com.doverfuelingsolutions.issp.view.fragment.FragmentInit
import com.doverfuelingsolutions.issp.view.fragment.FragmentSelect
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import kotlinx.coroutines.launch
import java.io.File

class MainActivity : AppCompatActivity(), OnFusionEvent {

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

    private val binding: ActivityMainBinding by lazy { DataBindingUtil.setContentView(this, R.layout.activity_main) }
    private val mainViewModel: MainViewModel by viewModels()

    private val fragmentInit = FragmentInit()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.lifecycleOwner = this
        binding.mainViewModel = mainViewModel
        initView()
        initFusion()
    }

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

    override fun onFusionInit(code: FusionError, msg: String) {
        lifecycleScope.launch {
            if (code != FusionError.Success) {
                fragmentInit.fail(msg)
                return@launch
                MaterialAlertDialogBuilder(this@MainActivity)
                    .setTitle(R.string.tip_opt_fail)
                    .setCancelable(false)
                    .setMessage(StringUtil.get(R.string.tip_connect_fail_reason_config, msg))
                    .setPositiveButton(R.string.go_check) { _, _ ->
                        PreferenceActivity.start(this@MainActivity)
                    }
                    .show()
            }
        }
    }

    private fun initView() {
        supportActionBar?.hide()
        mainViewModel.stationName.value = GlobalData.businessName.get()
        if (GlobalData.stationLogoFileName.get().isNotEmpty())
            binding.stationLogo.setImageURI(Uri.fromFile(File(filesDir, GlobalData.stationLogoFileName.get())))
        binding.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)
                    }
                }
        }
    }

    private fun initFusion() {
        supportFragmentManager.beginTransaction()
            .add(R.id.fragmentBox, fragmentInit)
            .commit()

        FusionManager.onFusionEvent = this
        lifecycle.addObserver(FusionManager)
    }

    class MainViewModel : ViewModel() {
        val stationName = MutableLiveData("")
    }
}