package com.atridad.ascently.utils

import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import androidx.annotation.RequiresApi
import com.atridad.ascently.MainActivity
import com.atridad.ascently.R

object AppShortcutManager {

    const val SHORTCUT_START_SESSION = "start_session"
    const val SHORTCUT_END_SESSION = "end_session"

    const val ACTION_START_SESSION = "com.atridad.ascently.action.START_SESSION"
    const val ACTION_END_SESSION = "com.atridad.ascently.action.END_SESSION"

    /** Updates the app shortcuts based on current session state */
    fun updateShortcuts(
        context: Context,
        hasActiveSession: Boolean,
        hasGyms: Boolean,
        lastUsedGym: com.atridad.ascently.data.model.Gym? = null,
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            val shortcutManager = context.getSystemService(ShortcutManager::class.java)

            val shortcuts = mutableListOf<ShortcutInfo>()

            if (hasActiveSession) {
                // Show "End Session" shortcut when there's an active session
                shortcuts.add(createEndSessionShortcut(context))
            } else if (hasGyms) {
                // Show "Start Session" shortcut when no active session but gyms exist
                shortcuts.add(createStartSessionShortcut(context, lastUsedGym))
            }

            shortcutManager.dynamicShortcuts = shortcuts
        }
    }

    @RequiresApi(Build.VERSION_CODES.N_MR1)
    private fun createStartSessionShortcut(
        context: Context,
        lastUsedGym: com.atridad.ascently.data.model.Gym? = null,
    ): ShortcutInfo {
        val startIntent =
            Intent(context, MainActivity::class.java).apply {
                action = ACTION_START_SESSION
                flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
                lastUsedGym?.let { gym -> putExtra("LAST_USED_GYM_ID", gym.id) }
            }

        val shortLabel =
            if (lastUsedGym != null) {
                "Start at ${lastUsedGym.name}"
            } else {
                context.getString(R.string.shortcut_start_session_short)
            }

        val longLabel =
            if (lastUsedGym != null) {
                "Start a new climbing session at ${lastUsedGym.name}"
            } else {
                context.getString(R.string.shortcut_start_session_long)
            }

        return ShortcutInfo.Builder(context, SHORTCUT_START_SESSION)
            .setShortLabel(shortLabel)
            .setLongLabel(longLabel)
            .setIcon(Icon.createWithResource(context, R.drawable.ic_play_arrow_24))
            .setIntent(startIntent)
            .build()
    }

    @RequiresApi(Build.VERSION_CODES.N_MR1)
    private fun createEndSessionShortcut(context: Context): ShortcutInfo {
        val endIntent =
            Intent(context, MainActivity::class.java).apply {
                action = ACTION_END_SESSION
                flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
            }

        return ShortcutInfo.Builder(context, SHORTCUT_END_SESSION)
            .setShortLabel(context.getString(R.string.shortcut_end_session_short))
            .setLongLabel(context.getString(R.string.shortcut_end_session_long))
            .setIcon(Icon.createWithResource(context, R.drawable.ic_stop_24))
            .setIntent(endIntent)
            .build()
    }

    /** Removes all dynamic shortcuts */
    fun clearShortcuts(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            val shortcutManager = context.getSystemService(ShortcutManager::class.java)
            shortcutManager.removeAllDynamicShortcuts()
        }
    }

    /** Disables a specific shortcut and shows a disabled message */
    fun disableShortcut(context: Context, shortcutId: String, disabledMessage: String) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            val shortcutManager = context.getSystemService(ShortcutManager::class.java)
            shortcutManager.disableShortcuts(listOf(shortcutId), disabledMessage)
        }
    }
}
