import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smart_hotel_app/views/home/inspection/cubit/inspection_state.dart';

class InspectionCubit extends Cubit<InspectionState> {
  static const List<String> allRooms = [
    '101',
    '102',
    '103',
    '104',
    '105',
    '106',
  ];

  static final List<InspectionDevice> allDevices = [
    InspectionDevice(
      id: '1',
      icon: Icons.wifi,
      name: '101主控网关',
      type: '智能网关',
      location: '101电视柜',
      status: '正常',
      time: '2024-01-15 14:30:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
    InspectionDevice(
      id: '2',
      icon: Icons.thermostat,
      name: '温湿度传感器-101',
      type: '传感器',
      location: '101角落',
      status: '正常',
      time: '2024-01-15 14:25:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
    InspectionDevice(
      id: '3',
      icon: Icons.videocam,
      name: '智能摄像头-102',
      type: '摄像头',
      location: '102门口',
      status: '告警',
      time: '2024-01-15 10:00:00',
      statusColor: const Color.fromRGBO(255, 77, 79, 1.0),
    ),
    InspectionDevice(
      id: '4',
      icon: Icons.light,
      name: '智能吸顶灯-102',
      type: '传感器',
      location: '102天花板',
      status: '正常',
      time: '2024-01-15 14:25:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
    InspectionDevice(
      id: '5',
      icon: Icons.thermostat,
      name: '温湿度传感器-103',
      type: '传感器',
      location: '103角落',
      status: '正常',
      time: '2024-01-15 14:25:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
    InspectionDevice(
      id: '6',
      icon: Icons.light,
      name: '103灯-智能',
      type: '智能灯',
      location: '103',
      status: '开启',
      time: '2024-01-15 14:20:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
    InspectionDevice(
      id: '7',
      icon: Icons.sensor_door,
      name: '门窗传感器-104',
      type: '传感器',
      location: '104门',
      status: '离线',
      time: '2024-01-15 12:00:00',
      statusColor: const Color.fromRGBO(102, 102, 102, 1.0),
    ),
    InspectionDevice(
      id: '8',
      icon: Icons.kitchen,
      name: '智能插座-105',
      type: '插座',
      location: '105台面',
      status: '关闭',
      time: '2024-01-15 14:15:00',
      statusColor: const Color.fromRGBO(100, 116, 139, 1.0),
    ),
    InspectionDevice(
      id: '9',
      icon: Icons.smoke_free,
      name: '烟雾报警器-105',
      type: '报警器',
      location: '105天花板',
      status: '正常',
      time: '2024-01-15 14:30:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
    InspectionDevice(
      id: '10',
      icon: Icons.desk,
      name: '智能台灯-106',
      type: '智能灯',
      location: '106书桌',
      status: '关闭',
      time: '2024-01-15 14:10:00',
      statusColor: const Color.fromRGBO(100, 116, 139, 1.0),
    ),
    InspectionDevice(
      id: '11',
      icon: Icons.ac_unit,
      name: '智能空调-101',
      type: '空调',
      location: '101',
      status: '正常',
      time: '2024-01-15 14:30:00',
      statusColor: const Color.fromRGBO(26, 188, 156, 1.0),
    ),
  ];

  InspectionCubit() : super(const InspectionState()) {
    initData();
  }

  void initData() {
    emit(state.copyWith(deviceList: _filterDevices()));
  }

  void selectTab(DeviceStatusTab tab) {
    emit(state.copyWith(selectedTab: tab, deviceList: _filterDevices(selectedTab: tab)));
  }

  void toggleRoom(String room) {
    final List<String> newSelectedRooms = List.from(state.selectedRooms);
    if (newSelectedRooms.contains(room)) {
      newSelectedRooms.remove(room);
    } else {
      newSelectedRooms.add(room);
    }
    emit(state.copyWith(
      selectedRooms: newSelectedRooms,
      deviceList: _filterDevices(selectedRooms: newSelectedRooms),
    ));
  }

  List<InspectionDevice> _filterDevices({
    DeviceStatusTab? selectedTab,
    List<String>? selectedRooms,
  }) {
    DeviceStatusTab tab = selectedTab ?? state.selectedTab;
    List<String> rooms = selectedRooms ?? state.selectedRooms;

    List<InspectionDevice> filtered = List.from(allDevices);

    if (rooms.isNotEmpty) {
      filtered = filtered.where((device) {
        return rooms.any((room) => device.location.contains(room) || device.name.contains(room));
      }).toList();
    }

    if (tab != DeviceStatusTab.total) {
      filtered = filtered.where((device) {
        switch (tab) {
          case DeviceStatusTab.on:
            return device.status == '正常' || device.status == '开启';
          case DeviceStatusTab.off:
            return device.status == '关闭';
          case DeviceStatusTab.offline:
            return device.status == '离线';
          case DeviceStatusTab.alarm:
            return device.status == '告警';
          case DeviceStatusTab.fault:
            return device.status == '故障';
          default:
            return true;
        }
      }).toList();
    }

    return filtered;
  }
}
