package com.swago.app

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import com.alibaba.android.arouter.launcher.ARouter
import com.permissionx.guolindev.PermissionX
import com.swago.baseswago.constant.ARouteConstant

class SplashActivity : AppCompatActivity() {

    private val splashVm by viewModels<SplashVm>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        PermissionX.init(this@SplashActivity)
            .permissions(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    splashVm.getApiDomain()
                    splashVm.jumpToLoginLiveData.observe(this){
                        if (!it){
                            ARouter.getInstance().build(ARouteConstant.Login.login).navigation()
                            finish()
                        }
                    }
                } else {
                    Toast.makeText(
                        this@SplashActivity,
                        "These permissions are denied: $deniedList",
                        Toast.LENGTH_LONG
                    ).show()
                    ARouter.getInstance().build(ARouteConstant.Login.login).navigation()
                    finish()
                }
            }
    }
}