package com.android.launcher3.function.pwd

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.view.View
import com.android.launcher3.BaseActivity
import com.android.launcher3.R
import com.android.launcher3.function.pwd.fragment.EncryptedSetFragment
import com.android.launcher3.function.pwd.fragment.EncryptedVerifyFragment
import com.secspace.lib.common.utils.Prefs


class EncryptedActivity : BaseActivity() {
    private var mContext: Context? = null
    private val encryptedSet = EncryptedSetFragment()
    private val encryptedVer = EncryptedVerifyFragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setAndroidNativeLightStatusBar(true)
        setContentView(R.layout.activity_encrypted)
        mContext = this
        val from = intent.getStringExtra("from")
        if(from == null){
            val encrypted = Prefs.getEncrypted(mContext)
            if (encrypted == "") {
                replaceFragment(encryptedSet)
            } else {
                replaceFragment(encryptedVer)
            }
            return
        }
        replaceFragment(encryptedSet)

    }

    private fun replaceFragment(fragment: Fragment) {
        val fragmentManager: FragmentManager = supportFragmentManager
        val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()

        fragmentTransaction.replace(R.id.ll_encrypted_parent, fragment, "")
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }

    fun close(view: View?) {
        finish()
    }

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