import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:laki_icu_app/blocs/bluetooth_read/bluetooth_read_bloc.dart';
import 'package:laki_icu_app/blocs/bluetooth_read/bluetooth_read_state.dart';

import '../../cubit/settings_index_cubit.dart';
import '../../cubit/settings_index_state.dart';
import '../../widgets/settings_panel_frame.dart';

class SettingsAboutPanel extends StatelessWidget {
  const SettingsAboutPanel({super.key});

  @override
  Widget build(BuildContext context) {
    return SettingsPanelFrame(
      backgroundAsset: settingsAssetPanelMain,
      padding: EdgeInsets.fromLTRB(42.w, 54.h, 42.w, 48.h),
      child: BlocBuilder<SettingsIndexCubit, SettingsIndexState>(
        builder: (context, state) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const _LogoPlaceholder(),
              SizedBox(height: 62.h),
              Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Expanded(child: _ProductTitle()),
                  SizedBox(width: 42.w),
                  const _BarcodeCard(),
                ],
              ),
              SizedBox(height: 36.h),
              Divider(color: const Color(0xFF4CA7D2).withValues(alpha: 0.36)),
              SizedBox(height: 18.h),
              Expanded(
                child: Row(
                  children: [
                    Expanded(child: _DeviceInfoColumn()),
                    Container(
                      width: 1.w,
                      margin: EdgeInsets.symmetric(horizontal: 36.w),
                      color: const Color(0xFF3A9BD2).withValues(alpha: 0.42),
                    ),
                    Expanded(
                      child: _PreferenceColumn(
                        state: state,
                        onTemperatureUnitChanged: context
                            .read<SettingsIndexCubit>()
                            .selectTemperatureUnit,
                        onLockChanged:
                            context.read<SettingsIndexCubit>().setLockScreen,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}

class _LogoPlaceholder extends StatelessWidget {
  const _LogoPlaceholder();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 855.w,
        height: 184.h,
        color: const Color(0xFFD7D7D7),
        alignment: Alignment.center,
        child: Text(
          'LOGO',
          style: TextStyle(
            color: Colors.black,
            fontSize: 32.sp,
            fontWeight: FontWeight.w800,
          ),
        ),
      ),
    );
  }
}

class _ProductTitle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'TSAAS ICU 动物医疗监护舱',
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            color: Colors.white,
            fontSize: 42.sp,
            fontWeight: FontWeight.w800,
          ),
        ),
        SizedBox(height: 18.h),
        Text(
          '730SE-CICU-A',
          style: TextStyle(
            color: Colors.white,
            fontSize: 34.sp,
            fontWeight: FontWeight.w800,
          ),
        ),
      ],
    );
  }
}

class _BarcodeCard extends StatelessWidget {
  const _BarcodeCard();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 400.w,
      height: 150.h,
      padding: EdgeInsets.fromLTRB(28.w, 20.h, 28.w, 14.h),
      decoration: BoxDecoration(
        color: const Color(0xFFF4F4F4),
        borderRadius: BorderRadius.circular(18.r),
      ),
      child: Column(
        children: [
          Expanded(child: CustomPaint(painter: _BarcodePainter())),
          SizedBox(height: 6.h),
          Text(
            'ICU-A03-2026-0601-001',
            style: TextStyle(
              color: const Color(0xFF1B1B1B),
              fontSize: 14.sp,
              fontWeight: FontWeight.w700,
            ),
          ),
        ],
      ),
    );
  }
}

class _DeviceInfoColumn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<BluetoothReadBloc, BluetoothReadState>(
      buildWhen: (previous, current) => previous.info.sn != current.info.sn,
      builder: (context, state) {
        final sn =
            state.info.sn?.isNotEmpty == true ? state.info.sn! : 'CNA07212L';
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _InfoRow(label: '序列号:', value: sn),
            const _InfoRow(label: '设备码:', value: 'TS8P2603CUA019CN'),
            const _InfoRow(label: '设备激活时间:', value: '2026-05-11'),
            const _InfoRow(label: '保修截止时间:', value: '2027-05-11'),
          ],
        );
      },
    );
  }
}

class _PreferenceColumn extends StatelessWidget {
  const _PreferenceColumn({
    required this.state,
    required this.onTemperatureUnitChanged,
    required this.onLockChanged,
  });

  final SettingsIndexState state;
  final ValueChanged<bool> onTemperatureUnitChanged;
  final ValueChanged<bool> onLockChanged;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        _InfoRow(
          label: '语言:',
          valueWidget: _SelectButton(text: state.language),
        ),
        _InfoRow(
          label: '温度单位:',
          valueWidget: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              _UnitButton(
                text: '℃',
                active: state.useCelsius,
                onTap: () => onTemperatureUnitChanged(true),
              ),
              SizedBox(width: 22.w),
              _UnitButton(
                text: '℉',
                active: !state.useCelsius,
                onTap: () => onTemperatureUnitChanged(false),
              ),
            ],
          ),
        ),
        _InfoRow(
          label: '锁屏:',
          valueWidget: _SettingsSwitch(
            value: state.lockScreen,
            onChanged: onLockChanged,
          ),
        ),
        const _InfoRow(
          label: '缓存:',
          valueWidget: _ClearButton(),
        ),
      ],
    );
  }
}

class _InfoRow extends StatelessWidget {
  const _InfoRow({
    required this.label,
    this.value,
    this.valueWidget,
  });

  final String label;
  final String? value;
  final Widget? valueWidget;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        SizedBox(
          width: 210.w,
          child: Text(
            label,
            style: TextStyle(
              color: Colors.white,
              fontSize: 27.sp,
              fontWeight: FontWeight.w700,
            ),
          ),
        ),
        Expanded(
          child: Align(
            alignment: Alignment.centerRight,
            child: valueWidget ??
                Text(
                  value ?? '',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 27.sp,
                    fontWeight: FontWeight.w700,
                  ),
                ),
          ),
        ),
      ],
    );
  }
}

class _SelectButton extends StatelessWidget {
  const _SelectButton({required this.text});

  final String text;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 350.w,
      height: 52.h,
      decoration: settingsButtonDecoration(),
      padding: EdgeInsets.symmetric(horizontal: 28.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            text,
            style: TextStyle(
              color: Colors.white,
              fontSize: 24.sp,
              fontWeight: FontWeight.w700,
            ),
          ),
          SizedBox(width: 28.w),
          Icon(
            Icons.keyboard_arrow_down,
            color: const Color(0xFF7CCDF0).withValues(alpha: 0.74),
            size: 30.w,
          ),
        ],
      ),
    );
  }
}

class _UnitButton extends StatelessWidget {
  const _UnitButton({
    required this.text,
    required this.active,
    required this.onTap,
  });

  final String text;
  final bool active;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Container(
        width: 110.w,
        height: 52.h,
        decoration: settingsButtonDecoration(active: active),
        alignment: Alignment.center,
        child: Text(
          text,
          style: TextStyle(
            color: Colors.white,
            fontSize: 27.sp,
            fontWeight: FontWeight.w800,
          ),
        ),
      ),
    );
  }
}

class _SettingsSwitch extends StatelessWidget {
  const _SettingsSwitch({
    required this.value,
    required this.onChanged,
  });

  final bool value;
  final ValueChanged<bool> onChanged;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => onChanged(!value),
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 180),
        width: 92.w,
        height: 40.h,
        padding: EdgeInsets.all(3.w),
        decoration: BoxDecoration(
          color: const Color(0xFF062444),
          borderRadius: BorderRadius.circular(22.r),
          border: Border.all(color: const Color(0xFF58BCE8), width: 1.w),
        ),
        alignment: value ? Alignment.centerRight : Alignment.centerLeft,
        child: Container(
          width: 34.w,
          height: 34.w,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            gradient: const LinearGradient(
              colors: [Color(0xFFB9F4FF), Color(0xFF3189D9)],
            ),
            boxShadow: [
              BoxShadow(
                color: const Color(0xFF5ED4FF).withValues(alpha: 0.45),
                blurRadius: 10.r,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class _ClearButton extends StatelessWidget {
  const _ClearButton();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 110.w,
      height: 52.h,
      decoration: settingsButtonDecoration(),
      alignment: Alignment.center,
      child: Text(
        '清理',
        style: TextStyle(
          color: Colors.white,
          fontSize: 24.sp,
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }
}

class _BarcodePainter extends CustomPainter {
  final List<int> widths = const [
    2,
    1,
    4,
    1,
    1,
    3,
    2,
    4,
    1,
    2,
    3,
    1,
    4,
    2,
    1,
    3,
    2,
    1,
    4,
    3,
    1,
    2,
    4,
    1,
    3,
    2,
    1,
    4,
    2,
    3,
    1,
    2,
  ];

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..color = const Color(0xFF06101A);
    final total =
        widths.fold<int>(0, (sum, item) => sum + item) + widths.length;
    final unit = size.width / total;
    var x = 0.0;
    for (var index = 0; index < widths.length; index++) {
      final width = widths[index] * unit;
      if (index.isEven) {
        canvas.drawRect(Rect.fromLTWH(x, 0, width, size.height), paint);
      }
      x += width + unit;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
