import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/views/service/device/cubit/service_device_cubit.dart';
import 'package:smart_hotel_app/views/service/device/cubit/service_device_state.dart';
import 'package:smart_hotel_app/views/service/device/widget/mode_selection_block.dart';
import 'package:smart_hotel_app/views/service/device/widget/temperature_control_block.dart';
import 'package:smart_hotel_app/views/service/device/widget/wind_speed_block.dart';

@RoutePage()
class ServiceDeviceDetailView extends StatelessWidget {
  final int deviceId;

  const ServiceDeviceDetailView({super.key, required this.deviceId});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => ServiceDeviceCubit(deviceId: deviceId),
      child: Scaffold(
        backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios,
                color: Color.fromRGBO(100, 116, 139, 1)),
            onPressed: () {
              context.popRoute();
            },
          ),
          title: Text(
            '空调控制',
            style: TextStyle(
              color: const Color.fromRGBO(10, 13, 20, 1),
              fontSize: 32.sp,
              fontWeight: FontWeight.w200,
            ),
          ),
          centerTitle: true,
        ),
        body: BlocBuilder<ServiceDeviceCubit, ServiceDeviceState>(
          builder: (context, state) {
            final cubit = context.read<ServiceDeviceCubit>();
            if (state.isLoading) {
              return const Center(child: CircularProgressIndicator());
            }
            if (state.error != null) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '加载失败: ${state.error}',
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: const Color.fromRGBO(100, 116, 139, 1),
                      ),
                    ),
                    SizedBox(height: 16.h),
                    ElevatedButton(
                      onPressed: () => cubit.loadAcStatus(),
                      child: const Text('重试'),
                    ),
                  ],
                ),
              );
            }
            return SingleChildScrollView(
              padding: EdgeInsets.symmetric(horizontal: 28.w),
              child: Column(
                children: [
                  SizedBox(height: 20.h),
                  TemperatureControlBlock(
                    isOn: state.isOn,
                    targetTemperature: state.targetTemperature,
                    currentTemperature: state.currentTemperature,
                    minTemperature: state.minTemperature,
                    maxTemperature: state.maxTemperature,
                    onTogglePower: cubit.togglePower,
                    onIncreaseTemperature: cubit.increaseTemperature,
                    onDecreaseTemperature: cubit.decreaseTemperature,
                  ),
                  SizedBox(height: 20.h),
                  ModeSelectionBlock(
                    modeData: state.modeData,
                    onSelectMode: cubit.selectMode,
                  ),
                  SizedBox(height: 20.h),
                  WindSpeedBlock(
                    currentSpeed: state.currentSpeed,
                    speedOptions: state.speedOptions,
                    onSetWindSpeed: cubit.setWindSpeed,
                  ),
                  SizedBox(height: 20.h),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
