package com.amz.genie.activities

import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import com.amz.genie.R
import com.amz.genie.helpers.Preferences
import com.amz.genie.helpers.Utils.forceLogoutAndGoLogin
import com.amz.genie.helpers.Utils.isFemale
import com.amz.genie.helpers.Utils.uriToBase64
import com.amz.genie.models.Pengguna
import com.google.android.material.imageview.ShapeableImageView
import com.google.gson.Gson

class MoreActivity : BaseActivity() {

    private lateinit var ibBack: ImageButton
    private lateinit var ibBrowsePicture: ImageButton
    private lateinit var btLogout: Button
    private lateinit var sivEmployee: ShapeableImageView
    private lateinit var tvName: TextView
    private lateinit var tvJobDesk: TextView
    private lateinit var pickImageLauncher: ActivityResultLauncher<String>
    private var base64Picture = ""

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

        initUI()

    }

    private fun initUI() {
        ibBack = findViewById(R.id.ib_back_more)
        ibBrowsePicture = findViewById(R.id.ib_edit_picture_more)
        btLogout = findViewById(R.id.bt_logout_more)
        sivEmployee = findViewById(R.id.siv_employee_more)
        tvJobDesk = findViewById(R.id.tv_jobdesk_more)
        tvName = findViewById(R.id.tv_name_more)

        initData()
        setupActions()
    }

    private fun setupActions() {
        ibBack.setOnClickListener { handleBackPress(0) }

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() = handleBackPress(0)
        })

        pickImageLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
            uri?.let {
                sivEmployee.setImageURI(it)
                base64Picture = uriToBase64(this, it).toString()
            }
        }

        btLogout.setOnClickListener {
            showLogoutDialog()
        }

        ibBrowsePicture.setOnClickListener {
            pickImageLauncher.launch("image/*")
        }
    }

    private fun initData() {
        val data = Gson().fromJson(Preferences.getUserData(this),
            Pengguna::class.java)

        tvName.text = data.pegawai?.nama
        tvJobDesk.text = data.pegawai?.jabatan?.nama
        val female = isFemale(data.pegawai?.id_kelamin)
        sivEmployee.setImageResource(if (female) R.drawable.ic_woman else R.drawable.ic_man)
    }

    private fun showLogoutDialog() {
        AlertDialog.Builder(this)
            .setTitle("Logout")
            .setMessage("Yakin ingin keluar dari akun ini?")
            .setNegativeButton("Batal") { dialog, _ ->
                dialog.dismiss()
            }
            .setPositiveButton("Logout") { dialog, _ ->
                dialog.dismiss()
                forceLogoutAndGoLogin(this@MoreActivity)
            }
            .show()
    }
}