import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:smart_hotel_app/views/service/device/cubit/service_device_state.dart';

class ServiceDeviceCubit extends Cubit<ServiceDeviceState> {
  ServiceDeviceCubit() : super(const ServiceDeviceState()) {
    initData();
  }

  void initData() {
    emit(state.copyWith(
      isOn: true,
      targetTemperature: 24,
      currentTemperature: 26,
      minTemperature: 16,
      maxTemperature: 34,
      modeData: [
        {'name': '制冷', 'icon': Icons.ac_unit, 'isSelected': true},
        {'name': '制热', 'icon': Icons.heat_pump, 'isSelected': false},
        {'name': '除湿', 'icon': Icons.water_drop, 'isSelected': false},
        {'name': '送风', 'icon': Icons.air, 'isSelected': false},
      ],
      currentSpeed: '自动',
      speedOptions: ['自动', '低', '中', '高'],
    ));
  }

  void togglePower(bool value) {
    emit(state.copyWith(isOn: value));
  }

  void setTargetTemperature(int temp) {
    if (temp >= state.minTemperature && temp <= state.maxTemperature) {
      emit(state.copyWith(targetTemperature: temp));
    }
  }

  void increaseTemperature() {
    if (state.targetTemperature < state.maxTemperature) {
      emit(state.copyWith(targetTemperature: state.targetTemperature + 1));
    }
  }

  void decreaseTemperature() {
    if (state.targetTemperature > state.minTemperature) {
      emit(state.copyWith(targetTemperature: state.targetTemperature - 1));
    }
  }

  void selectMode(int index) {
    final updatedModeData = state.modeData.map((mode) {
      return {...mode, 'isSelected': false};
    }).toList();
    updatedModeData[index]['isSelected'] = true;
    emit(state.copyWith(modeData: updatedModeData));
  }

  void setWindSpeed(String speed) {
    emit(state.copyWith(currentSpeed: speed));
  }
}