package com.example.variableaxlemonitor.bluetooth

import android.annotation.SuppressLint
import android.bluetooth.*
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.example.variableaxlemonitor.data.AxleData
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.util.*

class BluetoothManager(private val context: Context) {
    private val TAG = "BluetoothManager"
    private val androidBluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as android.bluetooth.BluetoothManager
    private val adapter: BluetoothAdapter? = androidBluetoothManager.adapter
    private val scanner = adapter?.bluetoothLeScanner
    
    private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

    // ESP32 BLE UART Service & Characteristic UUIDs
    private val SERVICE_UUID = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
    private val RX_CHAR_UUID = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
    private val CLIENT_CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")

    private val _isConnected = MutableStateFlow(false)
    val isConnected: StateFlow<Boolean> = _isConnected

    private val _lastMessage = MutableStateFlow("기기 선택 대기 중")
    val lastMessage: StateFlow<String> = _lastMessage

    private val _mtu = MutableStateFlow(23)
    val mtu: StateFlow<Int> = _mtu

    private val _axleData = MutableStateFlow(AxleData())
    val axleData: StateFlow<AxleData> = _axleData

    private val _dataReceivedTrigger = MutableSharedFlow<Unit>()
    val dataReceivedTrigger: SharedFlow<Unit> = _dataReceivedTrigger

    private val _foundDevices = MutableStateFlow<List<BluetoothDevice>>(emptyList())
    val foundDevices: StateFlow<List<BluetoothDevice>> = _foundDevices

    private var gatt: BluetoothGatt? = null

    private val scanCallback = object : ScanCallback() {
        @SuppressLint("MissingPermission")
        override fun onScanResult(callbackType: Int, result: ScanResult) {
            val device = result.device
            // BLE 기기만 필터링 (DEVICE_TYPE_LE 또는 DEVICE_TYPE_DUAL)
            if (device.name != null && (device.type == BluetoothDevice.DEVICE_TYPE_LE || device.type == BluetoothDevice.DEVICE_TYPE_DUAL)) {
                val currentList = _foundDevices.value
                if (currentList.none { it.address == device.address }) {
                    _foundDevices.value = currentList + device
                }
            }
        }
    }

    private val gattCallback = object : BluetoothGattCallback() {
        @SuppressLint("MissingPermission")
        override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
            Log.d(TAG, "onConnectionStateChange: status=$status, newState=$newState")
            if (status == BluetoothGatt.GATT_SUCCESS) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    _lastMessage.value = "GATT 연결 성공. MTU 요청 중..."
                    Log.d(TAG, "Connected to GATT server. Requesting MTU...")
                    Handler(Looper.getMainLooper()).post {
                        Toast.makeText(context, "블루투스 기기에 연결되었습니다.", Toast.LENGTH_SHORT).show()
                    }
                    gatt.requestMtu(512)
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    _lastMessage.value = "기기 연결 끊김"
                    Log.d(TAG, "Disconnected from GATT server.")
                    _isConnected.value = false
                    this@BluetoothManager.gatt?.close()
                    this@BluetoothManager.gatt = null
                }
            } else {
                _lastMessage.value = "연결 오류 (status=$status)"
                Log.e(TAG, "GATT error: status=$status")
                _isConnected.value = false
                gatt.close()
                this@BluetoothManager.gatt = null
            }
        }

        @SuppressLint("MissingPermission")
        override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
            Log.d(TAG, "onMtuChanged: mtu=$mtu, status=$status")
            _mtu.value = mtu
            _lastMessage.value = "MTU 설정 완료 ($mtu). 서비스 검색 시작..."
            // Discover services after MTU is set
            gatt.discoverServices()
        }

        @SuppressLint("MissingPermission")
        override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
            Log.d(TAG, "onServicesDiscovered: status=$status")
            if (status == BluetoothGatt.GATT_SUCCESS) {
                _lastMessage.value = "서비스 발견 성공. 특성 찾는 중..."
                var foundTarget = false
                val service = gatt.getService(SERVICE_UUID)
                val characteristic = service?.getCharacteristic(RX_CHAR_UUID)
                
                if (characteristic != null) {
                    _lastMessage.value = "UART 특성 발견. 구독 설정 중..."
                    enableNotifications(gatt, characteristic)
                    foundTarget = true
                } else {
                    Log.w(TAG, "Target UART UUID not found. Searching for any Notify characteristic...")
                    for (srv in gatt.services) {
                        for (char in srv.characteristics) {
                            if ((char.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                                Log.d(TAG, "Found Notify characteristic: ${char.uuid} in service ${srv.uuid}")
                                _lastMessage.value = "대체 특성 발견. 구독 설정 중..."
                                enableNotifications(gatt, char)
                                foundTarget = true
                                break
                            }
                        }
                        if (foundTarget) break
                    }
                }
                
                if (foundTarget) {
                    _isConnected.value = true
                } else {
                    _lastMessage.value = "데이터 수신 특성을 찾을 수 없음"
                    Log.e(TAG, "No suitable Notify characteristic found on device.")
                }
            } else {
                _lastMessage.value = "서비스 발견 실패 (status=$status)"
            }
        }

        override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray) {
            processReceivedData(value)
        }

        @Deprecated("Deprecated in Java")
        override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
            @Suppress("DEPRECATION")
            processReceivedData(characteristic.value)
        }

        override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
            Log.d(TAG, "onDescriptorWrite: status=$status, descriptor=${descriptor.uuid}")
            if (status == BluetoothGatt.GATT_SUCCESS) {
                _lastMessage.value = "통신 준비 완료. 데이터 수신 대기 중..."
            } else {
                _lastMessage.value = "구독 설정 실패 (status=$status)"
            }
        }
    }

    private fun processReceivedData(value: ByteArray?) {
        if (value == null) return
        
        val received = String(value).trim()
        if (received.isNotEmpty()) {
            Log.d(TAG, "Extracted line: '$received'")
            scope.launch {
                parseData(received)
            }
        }
    }

    @SuppressLint("MissingPermission")
    private fun enableNotifications(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
        val success = gatt.setCharacteristicNotification(characteristic, true)
        Log.d(TAG, "setCharacteristicNotification(${characteristic.uuid}) success: $success")
        
        val descriptor = characteristic.getDescriptor(CLIENT_CONFIG_DESCRIPTOR)
        if (descriptor != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                gatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
            } else {
                @Suppress("DEPRECATION")
                descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                @Suppress("DEPRECATION")
                gatt.writeDescriptor(descriptor)
            }
            Log.d(TAG, "Writing Descriptor to enable Notifications for ${characteristic.uuid}")
        } else {
            Log.e(TAG, "Client Config Descriptor not found for ${characteristic.uuid}")
        }
    }

    @SuppressLint("MissingPermission")
    fun startScan() {
        Log.d(TAG, "Starting BLE scan...")
        // 페어링된 기기 중 BLE 기기만 먼저 추가
        val pairedDevices = adapter?.bondedDevices ?: emptySet()
        _foundDevices.value = pairedDevices.filter { 
            it.type == BluetoothDevice.DEVICE_TYPE_LE || it.type == BluetoothDevice.DEVICE_TYPE_DUAL 
        }
        
        scanner?.startScan(scanCallback)
    }

    @SuppressLint("MissingPermission")
    fun stopScan() {
        Log.d(TAG, "Stopping BLE scan.")
        scanner?.stopScan(scanCallback)
    }

    @SuppressLint("MissingPermission")
    fun connect(device: BluetoothDevice) {
        Log.d(TAG, "Connecting to ${device.address} via TRANSPORT_LE")
        stopScan()
        disconnect()
        gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
    }

    private suspend fun parseData(line: String) {
        // Remove any non-numeric/comma characters except dots and minus
        val cleaned = line.replace(Regex("[^0-9,.-]"), "")
        if (cleaned.isEmpty()) return
        Log.d(TAG, "Parsing Cleaned Line: '$cleaned'")
        
        val parts = cleaned.split(",").filter { it.isNotEmpty() }.map { it.trim() }
        if (parts.size >= 5) {
            try {
                val pRawStr = parts[0]
                val sRawStr = parts[1]
                val vRawStr = parts[2]
                val dRawStr = parts[3]
                val iRawStr = parts[4]
                
                Log.d(TAG, "Part Strings: P=$pRawStr, S=$sRawStr, V=$vRawStr, D=$dRawStr, I=$iRawStr")

                // Parse as Int first as requested by user
                val pInt = pRawStr.toIntOrNull() ?: 0
                val sInt = sRawStr.toIntOrNull() ?: 0
                val vInt = vRawStr.toIntOrNull() ?: 0
                val dInt = dRawStr.toIntOrNull() ?: 0
                val iInt = iRawStr.toIntOrNull() ?: 0

                val data = AxleData(
                    pressure = pInt,
                    speed = sInt,
                    variableAxleLoad = vInt,
                    driveAxleLoad = dInt,
                    isDown = iInt == 1,
                    timestamp = System.currentTimeMillis()
                )
                
                Log.d(TAG, "Successfully Parsed Raw Data: $data")
                
                // Use update block for better StateFlow awareness
                _axleData.update { data }
                
                // Update on Main thread to ensure UI recomposition
                withContext(Dispatchers.Main) {
                    _dataReceivedTrigger.emit(Unit)
                }
            } catch (e: Exception) {
                Log.e(TAG, "Parse error: ${e.message} in '$cleaned'")
            }
        } else {
            Log.w(TAG, "Incomplete data parts (${parts.size}): $cleaned")
        }
    }

    @SuppressLint("MissingPermission")
    fun disconnect() {
        Log.d(TAG, "Disconnecting GATT")
        gatt?.disconnect()
        _isConnected.value = false
    }
}
