import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/horizontal_user_card.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/simple_Info.dart';

class DeviceSelectDialog extends FStatefulWidget implements FPage {
  @override
  String get pageName => "DeviceSelectDialog";

  ///设备列表
  final List<DeviceExtendInfoDTO> devices;

  final void Function(String) onOk;

  DeviceSelectDialog({required this.devices, required this.onOk});

  @override
  FState<FStatefulWidget> createState() {
    return _FDeviceSelectState();
  }
}

class _FDeviceSelectState extends FState<DeviceSelectDialog> {
  String _selectedCode = "";

  @override
  void initState() {
    if (widget.devices.isNotEmpty) {
      _selectedCode = widget.devices.first.deviceCode!;
    }
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    var title = FText(
      i18nBook.college.selectDevice.t,
      style: TextStyle(color: Colors.white),
    );
    FWidget titleContent = title;
    if (kIsMobile) {
      titleContent = FContainer(
        width: 400,
        child: FRow(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            title,
            _buildSubmitButton(),
          ],
        ),
      );
    }
    return FSimpleDialog(
      alignment: kIsMobile ? Alignment.topCenter : Alignment.center,
      isNeedClose: !kIsMobile,
      titlePadding: kIsMobile
          ? EdgeInsets.symmetric(vertical: 0)
          : EdgeInsets.fromLTRB(12.0, 12.0, 12.0, 12.0),
      insetPadding: kIsMobile
          ? EdgeInsets.symmetric(vertical: 0)
          : EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
      isDefault: !kIsMobile,
      bottomButton: _buildBttomButton(),
      onOk: () {
        widget.onOk.call(_selectedCode);
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      onCancel: router.back,
      onClose: router.back,
      title: title,
      children: [
        FContainer(
          width: 400,
          height: kIsMobile ? 250 : 500,
          child: FListView(
            children: widget.devices
                .map((e) => FHorizontalUserSimpleCard(
                      itemInfo: SimpleInfoItem(
                        itemCode: e.deviceCode!,
                        name: e.displayName,
                        role: e.shortCode,
                        des: [e.description],
                      ),
                      onTap: (isSelected) {
                        setState(() {
                          _selectedCode = e.deviceCode!;
                        });
                      },
                      isSelected: _selectedCode == e.deviceCode,
                    ))
                .toList(),
          ),
        )
      ],
    );
  }

  FWidget _buildSubmitButton() {
    return FElevatedButton(
      child: FText(i18nBook.common.confirm.t),
      onPressed: () {
        widget.onOk.call(_selectedCode);
      },
      businessParent: widget,
      name: i18nBook.common.confirm.t,
    );
  }

  FWidget _buildBttomButton() {
    return FContainer(
      alignment: Alignment.center,
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          FElevatedButton(
            child: FText(i18nBook.common.confirm.t),
            onPressed: () {
              widget.onOk.call(_selectedCode);
            },
            businessParent: widget,
            name: i18nBook.common.confirm.t,
          ),
          const FSizedBox(
            width: 15,
          ),
          FElevatedButton(
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all<Color>(Colors.white),
            ),
            child: FText(
              i18nBook.common.cancel.t,
              style: TextStyle(color: Colors.black),
            ),
            onPressed: () {
              router.back();
            },
            businessParent: widget,
            name: i18nBook.common.cancel.t,
          ),
        ],
      ),
    );
  }
}
