import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smart_hotel_app/views/report/room/cubit/room_report_state.dart';

class RoomReportCubit extends Cubit<RoomReportState> {
  RoomReportCubit() : super(const RoomReportState()) {
    _initData();
  }

  void _initData() {
    final floors = [
      FloorInfo(
        floorName: '一楼',
        rooms: List.generate(10, (index) => _createRoom(101 + index)),
      ),
      FloorInfo(
        floorName: '二楼',
        rooms: List.generate(10, (index) => _createRoom(201 + index)),
      ),
      FloorInfo(
        floorName: '三楼',
        rooms: List.generate(10, (index) => _createRoom(301 + index)),
      ),
      FloorInfo(
        floorName: '四楼',
        rooms: List.generate(10, (index) => _createRoom(401 + index)),
      ),
      FloorInfo(
        floorName: '五楼',
        rooms: List.generate(10, (index) => _createRoom(501 + index)),
      ),
      FloorInfo(
        floorName: '六楼',
        rooms: List.generate(10, (index) => _createRoom(601 + index)),
      ),
    ];

    final statusStats = [
      const StatusStat(
        label: '入住',
        count: 45,
        color: Color.fromRGBO(59, 130, 246, 1),
      ),
      const StatusStat(
        label: '空闲',
        count: 7,
        color: Color.fromRGBO(156, 163, 175, 1),
      ),
      const StatusStat(
        label: '预订',
        count: 5,
        color: Color.fromRGBO(147, 197, 253, 1),
      ),
      const StatusStat(
        label: '告警',
        count: 3,
        color: Color.fromRGBO(251, 191, 36, 1),
      ),
    ];

    final detailRooms = [
      RoomInfo(
        roomNumber: '302',
        status: RoomStatus.occupied,
        statusText: '入住中',
        deviceStatus: const RoomDeviceStatus(
          hasPerson: true,
          powerOn: true,
          wifiNormal: true,
          acOn: true,
          acTemperature: '24',
          lightOn: false,
          tvOn: true,
          curtainOn: false,
        ),
      ),
    ];

    emit(state.copyWith(
      floors: floors,
      statusStats: statusStats,
      detailRooms: detailRooms,
    ));
  }

  RoomInfo _createRoom(int number) {
    RoomStatus status;
    String statusText;

    if (number == 303) {
      status = RoomStatus.warning;
      statusText = '告警';
    } else if (number == 309) {
      status = RoomStatus.temperature;
      statusText = '温度';
    } else if ([302, 304, 308].contains(number)) {
      status = RoomStatus.occupied;
      statusText = number == 308 ? '入住·勿扰' : '入住·在房';
    } else if (number == 307) {
      status = RoomStatus.reserved;
      statusText = '预定';
    } else {
      status = RoomStatus.vacant;
      statusText = '空闲';
    }

    return RoomInfo(
      roomNumber: number.toString(),
      status: status,
      statusText: statusText,
    );
  }

  void selectFloor(int index) {
    emit(state.copyWith(selectedFloorIndex: index));
  }
}