package es.verdnatura.presentation.view.feature.login.activity

import android.content.Intent
import android.net.Uri
import es.verdnatura.MobileApplication
import es.verdnatura.R
import es.verdnatura.databinding.ActivityLoginBinding
import es.verdnatura.presentation.base.BaseActivity
import es.verdnatura.presentation.common.LoginListener
import es.verdnatura.presentation.common.TAG
import es.verdnatura.presentation.common.addFragment
import es.verdnatura.presentation.view.feature.login.fragment.LoginFragment
import es.verdnatura.presentation.view.feature.main.activity.MainActivity

class LoginActivity : BaseActivity<ActivityLoginBinding>(), LoginListener {
    private var imageUri: Uri? = null

    override fun getLayoutId(): Int = R.layout.activity_login

    override fun init() {
        (application as MobileApplication).setLoginListener(this)
        val intent = intent
        val action = intent.action
        val type = intent.type

        if (Intent.ACTION_SEND == action && type?.startsWith("image/") == true) {
            val clipData = intent.clipData
            if (clipData != null) {
                imageUri = clipData.getItemAt(0).uri
            }
        }

        addFragment(LoginFragment.newInstance(imageUri), R.id.main_frame_layout, LoginFragment.TAG)
    }

    fun goToMain() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    override fun onApplicationEventAnimation(visibility: Int) {
        binding.splashAnimation.visibility = visibility
    }

}

