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/home/device/cubit/device_cubit.dart';
import 'package:smart_hotel_app/views/home/device/cubit/device_state.dart';
import 'package:smart_hotel_app/views/home/device/widget/device_parms_block.dart';
import 'package:smart_hotel_app/views/home/device/widget/power_char_block.dart';
import 'package:smart_hotel_app/views/home/device/widget/state_info_block.dart';

@RoutePage()
class DeviceDetailView extends StatelessWidget {
  const DeviceDetailView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => DeviceCubit(),
      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: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 28.w),
          child: BlocBuilder<DeviceCubit, DeviceState>(
            builder: (context, state) {
              final cubit = context.read<DeviceCubit>();
              return Column(
                children: [
                  StateInfoBlock(deviceInfo: state.deviceInfo),
                  SizedBox(height: 10.h),
                  DeviceParmsBlock(deviceInfo: state.deviceInfo),
                  SizedBox(height: 10.h),
                  PowerCharBlock(deviceInfo: state.deviceInfo),
                  SizedBox(height: 10.h),
                  _buildBottomButtons(cubit),
                  SizedBox(height: 30.h),
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  Widget _buildBottomButtons(DeviceCubit cubit) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 15.h),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 300.w,
            height: 88.h,
            decoration: BoxDecoration(
              color: const Color.fromRGBO(73, 149, 234, 1),
              borderRadius: BorderRadius.circular(16.r),
            ),
            child: TextButton(
              onPressed: () => cubit.remotePowerOn(),
              child: Text(
                "远程送电",
                style: TextStyle(
                    fontSize: 28.sp,
                    color: const Color.fromRGBO(237, 245, 255, 1)),
              ),
            ),
          ),
          SizedBox(width: 30.w),
          Container(
            width: 300.w,
            height: 88.h,
            decoration: BoxDecoration(
              color: const Color.fromRGBO(229, 241, 255, 1.0),
              border: Border.all(
                color: const Color.fromRGBO(73, 149, 234, 1),
                width: 1.w,
              ),
              borderRadius: BorderRadius.circular(16.r),
            ),
            child: TextButton(
              onPressed: () => cubit.remotePowerOff(),
              child: Text(
                "远程断电",
                style: TextStyle(
                    fontSize: 28.sp,
                    color: const Color.fromRGBO(100, 116, 139, 1)),
              ),
            ),
          ),
        ],
      ),
    );
  }
}