package com.lemonhc.mplus.mcare_plus.activity.lockScreen

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import com.lemonhc.mplus.framework.constant.FrameworkConstant
import com.lemonhc.mplus.framework.util.PreferenceUtil
import com.lemonhc.mplus.framework.util.kotlinExtension.isCaptureFlag
import com.lemonhc.mplus.mcare_plus.R
import com.lemonhc.mplus.mcare_plus.activity.MainActivity

class LockOptionActivity: AppCompatActivity(){
    //http://recipes4dev.tistory.com/43 참고할 만한 사이트
    val LIST_MENU = arrayOf("화면잠금(필수)", "생체인증(지문)", "암호변경")
    val LIST_DESC = arrayOf("암호를 분실했을 경우 앱을 삭제하고 재설치 해야합니다.", "", "")
    val adapter:ListViewAdapter = ListViewAdapter(this)
    var listView:ListView? = null

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        if(isCaptureFlag == false){
            window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
        }
        setContentView(R.layout.activity_lock_option).run {
            listView = findViewById(R.id.lock_screen_option_listview)
        }

        listView?.adapter = adapter
        listView?.onItemClickListener = listItemClick()

        //생체정보 사용유무 저장된 값을 가져옴.
        val pref = this.getSharedPreferences("fingerprint", Context.MODE_PRIVATE)
        val savedFingerprintUseYn = pref.getBoolean("fingerprint", true)

        adapter.addItem(LIST_MENU[0], LIST_DESC[0], true, true, true) // 화면잠금
//        adapter.addItem(LIST_MENU[1], "", true, savedFingerprintUseYn, false) // 생체인증
        adapter.addItem(LIST_MENU[2], "", false, false, false) // 암호변경
        
    }

    fun listItemClick():AdapterView.OnItemClickListener{
        return AdapterView.OnItemClickListener{ parent, v, position, id ->

            // get item
            val item = parent.getItemAtPosition(position) as ListViewItem

            when (id.toInt()) {
                0 -> return@OnItemClickListener
//                1 -> return@OnItemClickListener
                1 -> {
                    // 암호변경화면으로 넘겨줌.
                    val intent = Intent(this, LockScreenActivity::class.java)
                    intent.putExtra("input_type", FrameworkConstant.passwordInputType.CHANGE_FIRST_INPUT)
                    startActivityForResult(intent, 1)
                }
            }
        }
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Log.e("!!!!", "Change Password")
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
            }
        }
    }

    public override fun onResume() {
        super.onResume()
        if (this.intent.hasExtra("lock_yn")) {
            val lockYn = this.intent.extras.getBoolean("lock_yn")
            if (lockYn) {
                lockScreenCall()
            } else {
                this.intent.putExtra("lock_yn", true)
            }
        } else {
            lockScreenCall()
        }

    }
    
    fun lockScreenCall() {
        val util = PreferenceUtil.getInstance(this)
        //현재시간이 락을 해제하고 1초가 지난경우에만 락스크린을 보여준다.
        //초기값은 -1
        if (util.unlockTime + 1000 < System.currentTimeMillis() || util.unlockTime.toInt() == -1) {
            val intent = Intent(this, LockScreenActivity::class.java) // MainActivity.class); //LockScreenActivity
            startActivity(intent)
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()

        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
        intent.putExtra("lock_yn", false)
        startActivity(intent)
    }
}