package com.example.projekta

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Base64
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import com.example.projekta.databinding.ActivityMenuBinding

class MenuActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMenuBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMenuBinding.inflate(layoutInflater)
        setContentView(binding.root)

        loadUserSidebar()

        // Buka Drawer
        binding.btnMenu.setOnClickListener {
            binding.drawerLayout.openDrawer(GravityCompat.START)
        }

        // Menu Profile
        binding.menuProfile.setOnClickListener {
            startActivity(Intent(this, ProfileActivity::class.java))
            binding.drawerLayout.closeDrawer(GravityCompat.START)
        }

        // Menu Logout
        binding.menuLogout.setOnClickListener {

            getSharedPreferences("LOGIN", MODE_PRIVATE).edit().clear().apply()
            getSharedPreferences("USER_DATA", MODE_PRIVATE).edit().clear().apply()

            val intent = Intent(this, LoginActivity::class.java)
            intent.flags =
                Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

            startActivity(intent)
            finish()
        }

        // Menu Pemupukan
        binding.menuPemupukan.setOnClickListener {
            startActivity(Intent(this, PemupukanActivity::class.java))
        }

        // Menu Panel Surya
        binding.menuPanelSurya.setOnClickListener {
            startActivity(Intent(this, PanelSuryaActivity::class.java))
        }

        // Menu Panduan
        binding.menuPanduan.setOnClickListener {
            startActivity(Intent(this, PanduanActivity::class.java))
        }

        // Menu Tentang
        binding.menuTentang.setOnClickListener {
            startActivity(Intent(this, TentangActivity::class.java))
        }

        // Menu Histori
        binding.menuHistori.setOnClickListener {
            startActivity(Intent(this, HistoriActivity::class.java))
        }

        // Tombol Back
        onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {

                    if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
                        binding.drawerLayout.closeDrawer(GravityCompat.START)
                    } else {
                        finishAffinity()
                    }
                }
            }
        )
    }

    // Refresh data sidebar setiap Activity kembali dibuka
    override fun onResume() {
        super.onResume()
        loadUserSidebar()
    }

    // Memuat nama dan foto profil pada sidebar
    private fun loadUserSidebar() {

        val sharedPref = getSharedPreferences("USER_DATA", Context.MODE_PRIVATE)

        val nama = sharedPref.getString("nama", "Nama Pengguna")
        val foto = sharedPref.getString("foto", "")

        binding.txtSidebarName.text = nama

        if (!foto.isNullOrEmpty()) {

            try {

                val cleanBase64 = foto
                    .replace("\n", "")
                    .replace("\r", "")
                    .replace(" ", "")

                val imageBytes = Base64.decode(cleanBase64, Base64.DEFAULT)

                val bitmap = BitmapFactory.decodeByteArray(
                    imageBytes,
                    0,
                    imageBytes.size
                )

                binding.imgSidebarProfile.setImageBitmap(bitmap)

            } catch (e: Exception) {

                binding.imgSidebarProfile.setImageResource(R.drawable.profileman)
            }

        } else {

            binding.imgSidebarProfile.setImageResource(R.drawable.profileman)
        }
    }
}