import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/index.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/device_live/device_live_list/controller.dart';
import 'package:get/get.dart';
import 'package:fis_theme/theme.dart';

class DeviceLogsDialog extends FStatefulWidget implements FPage {
  DeviceLogsDialog({
    required this.deviceCode,
  });
  final String deviceCode;
  @override
  String get pageName => 'DeviceLogsDialog';

  @override
  FState<DeviceLogsDialog> createState() => _DeviceLogsDialogState();
}

class _DeviceLogsDialogState extends FState<DeviceLogsDialog> {
  final controller = Get.find<DeviceLiveListDesktopController>();
  LogTimeEnum logTimeEnum = LogTimeEnum.Today;

  DateTime? _startTime = DateTime.now().subtract(Duration(days: 1)).toUtc();
  DateTime? _endTime = null;

  final GlobalKey _startKey = GlobalKey();
  final GlobalKey _endKey = GlobalKey();
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      contentPadding: EdgeInsets.symmetric(vertical: 25, horizontal: 25),
      title: FText(
        i18nBook.remoteControl.selectTimeRange.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm4Btn.t,
      onClose: () {
        router.back();
      },
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
            width: 400,
            margin: EdgeInsets.only(top: 40, bottom: 60),
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTimeSelect(),
              ],
            )),
      ],
      isDefault: false,
    );
  }

  /// 单选框
  FWidget _buildSingleSelect() {
    final select = FSelect<FSelectOptionModel, LogTimeEnum>(
      fontFamily: FTheme.ins.localeSetting.fontFamily,
      source: [
        FSelectOptionModel(
          title: i18nBook.remoteControl.past24Hours.t,
          value: LogTimeEnum.Today,
        ),
        FSelectOptionModel(
          title: i18nBook.remoteControl.pastWeek.t,
          value: LogTimeEnum.OneWeek,
        ),
        FSelectOptionModel(
          title: i18nBook.remoteControl.pastMonth.t,
          value: LogTimeEnum.OneMonth,
        ),
        FSelectOptionModel(
          title: i18nBook.remoteControl.pastYear.t,
          value: LogTimeEnum.OneYear,
        ),
        FSelectOptionModel(
          title: i18nBook.common.all.t,
          value: LogTimeEnum.All,
        ),
      ],
      height: 36,
      value: logTimeEnum,
      optionLabelExtractor: (data) => data.title,
      optionValueExtractor: (data) => data.value,
      onSelectChanged: (value, index) {
        logTimeEnum = value!;
        setState(() {});
      },
    );
    return FExpanded(child: select);
  }

  FWidget _buildTimeSelect() {
    return FRow(
      children: [
        _buildSingleSelect(),
        FContainer(
          margin: EdgeInsets.only(left: 10),
          height: 30,
          child: FElevatedButton(
            child: FText(i18nBook.common.confirm.t),
            onPressed: () {
              _currentTimeSubmit();
            },
            businessParent: this.widget,
            name: 'add package',
          ),
        )
      ],
    );
  }

  DateTime _getTimeFrame(LogTimeEnum logTimeEnum) {
    DateTime now = DateTime.now().toUtc();

    switch (logTimeEnum) {
      case LogTimeEnum.Today:
        return now.subtract(Duration(days: 1));
      case LogTimeEnum.OneWeek:
        return now.subtract(Duration(days: 7));
      case LogTimeEnum.OneMonth:
        return DateTime(
          now.year,
          now.month - 1,
          now.day,
          now.hour,
          now.minute,
          now.second,
        );
      case LogTimeEnum.OneYear:
        return DateTime(
          now.year - 1,
          now.month,
          now.day,
          now.hour,
          now.minute,
          now.second,
        );
      case LogTimeEnum.All:
        return DateTime.utc(1, 1, 1);

      default:
        return now;
    }
  }

  void _currentTimeSubmit() {
    controller.state.isGettingLogs = true;
    controller.state.isPushDevicePatch = false;

    controller.getRemoteLogAsync(
      deviceCode: widget.deviceCode,
      logTimeEnum: logTimeEnum,
    );
  }

  /// 单行时间选择框
  FWidget _buildTimeItem({
    required String title,
    required FWidget filterWidget,
    bool isExpanded = false,
  }) {
    return FContainer(
      margin: EdgeInsets.symmetric(
        horizontal: 30,
        vertical: 10,
      ),
      child: FRow(
        children: [
          FExpanded(
            child: FContainer(
              child: FRow(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  FSizedBox(
                    width: 100,
                    child: FText(
                      title,
                    ),
                  ),
                  FSizedBox(
                    width: 20,
                  ),
                  if (isExpanded) ...[
                    filterWidget
                  ] else ...[
                    FExpanded(
                      child: filterWidget,
                    ),
                  ]
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
