package com.tt.honeyDue.platform

import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.view.HapticFeedbackConstants
import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView

/**
 * Android implementation of haptic feedback using system vibrator.
 */
class AndroidHapticFeedbackPerformer(
    private val view: View,
    private val vibrator: Vibrator?
) : HapticFeedbackPerformer {

    override fun perform(type: HapticFeedbackType) {
        // First try View-based haptic feedback (works best)
        val hapticConstant = when (type) {
            HapticFeedbackType.Light -> HapticFeedbackConstants.KEYBOARD_TAP
            HapticFeedbackType.Medium -> HapticFeedbackConstants.CONTEXT_CLICK
            HapticFeedbackType.Heavy -> HapticFeedbackConstants.LONG_PRESS
            HapticFeedbackType.Selection -> HapticFeedbackConstants.CLOCK_TICK
            HapticFeedbackType.Success -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                HapticFeedbackConstants.CONFIRM
            } else {
                HapticFeedbackConstants.CONTEXT_CLICK
            }
            HapticFeedbackType.Warning -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                HapticFeedbackConstants.REJECT
            } else {
                HapticFeedbackConstants.LONG_PRESS
            }
            HapticFeedbackType.Error -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                HapticFeedbackConstants.REJECT
            } else {
                HapticFeedbackConstants.LONG_PRESS
            }
        }

        val success = view.performHapticFeedback(hapticConstant)

        // Fallback to vibrator if view-based feedback fails
        if (!success && vibrator?.hasVibrator() == true) {
            performVibration(type)
        }
    }

    private fun performVibration(type: HapticFeedbackType) {
        vibrator ?: return

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val effect = when (type) {
                HapticFeedbackType.Light -> VibrationEffect.createOneShot(10, VibrationEffect.DEFAULT_AMPLITUDE)
                HapticFeedbackType.Medium -> VibrationEffect.createOneShot(20, VibrationEffect.DEFAULT_AMPLITUDE)
                HapticFeedbackType.Heavy -> VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE)
                HapticFeedbackType.Selection -> VibrationEffect.createOneShot(5, VibrationEffect.DEFAULT_AMPLITUDE)
                HapticFeedbackType.Success -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
                } else {
                    VibrationEffect.createOneShot(30, VibrationEffect.DEFAULT_AMPLITUDE)
                }
                HapticFeedbackType.Warning -> VibrationEffect.createOneShot(40, VibrationEffect.DEFAULT_AMPLITUDE)
                HapticFeedbackType.Error -> VibrationEffect.createOneShot(60, VibrationEffect.DEFAULT_AMPLITUDE)
            }
            vibrator.vibrate(effect)
        } else {
            @Suppress("DEPRECATION")
            val duration = when (type) {
                HapticFeedbackType.Light -> 10L
                HapticFeedbackType.Medium -> 20L
                HapticFeedbackType.Heavy -> 50L
                HapticFeedbackType.Selection -> 5L
                HapticFeedbackType.Success -> 30L
                HapticFeedbackType.Warning -> 40L
                HapticFeedbackType.Error -> 60L
            }
            @Suppress("DEPRECATION")
            vibrator.vibrate(duration)
        }
    }
}

@Composable
actual fun rememberHapticFeedback(): HapticFeedbackPerformer {
    val context = LocalContext.current
    val view = LocalView.current

    return remember {
        val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as? VibratorManager
            vibratorManager?.defaultVibrator
        } else {
            @Suppress("DEPRECATION")
            context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
        }
        AndroidHapticFeedbackPerformer(view, vibrator)
    }
}
