import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/rpc.dart';
import 'package:flyinsono/utils/export_status_type.dart';
import 'package:flyinsono/views/desktop/user_setting/controller.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

enum ExportDateEnum {
  lastOneDay,
  lastOneWeek,
  lastOneMonth,
}

class FExportLog extends FStatefulWidget {
  const FExportLog({super.key});

  @override
  FState<FExportLog> createState() => _FExportLogState();
}

class _FExportLogState extends FState<FExportLog> {
  /// 选中的拉取日志的时间
  ExportDateEnum selectExportDate = ExportDateEnum.lastOneDay;
  final controller = Get.find<UserSettingController>();

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      insetPadding: EdgeInsets.zero,
      backgroundColor: Colors.white,
      title: FText(
        i18nBook.setting.exportLog.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onOk: () {
        exportAppLogZip();
      },
      onCancel: () {
        router.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: true,
      children: [
        FContainer(
          margin: EdgeInsets.symmetric(horizontal: 30, vertical: 30),
          child: FRow(
            children: [
              FExpanded(
                child: _buildExportDateCard(
                  i18nBook.setting.lastOneDay.t,
                  ExportDateEnum.lastOneDay,
                ),
              ),
              FExpanded(
                child: _buildExportDateCard(
                  i18nBook.setting.lastOneWeek.t,
                  ExportDateEnum.lastOneWeek,
                ),
              ),
              FExpanded(
                child: _buildExportDateCard(
                  i18nBook.setting.lastOneMonth.t,
                  ExportDateEnum.lastOneMonth,
                ),
              )
            ],
          ),
        )
      ],
    );
  }

  FWidget _buildExportDateCard(
    String title,
    ExportDateEnum value,
  ) {
    bool isChecked = selectExportDate == value;
    FWidget _titleText;
    BoxDecoration _exportDateBoxDecoration;
    if (isChecked) {
      _titleText = FText(
        title,
        style: TextStyle(
          color: Colors.white,
        ),
      );
      _exportDateBoxDecoration = BoxDecoration(
        color: FTheme.ins.colorScheme.primary,
      );
    } else {
      _titleText = FText(
        title,
      );
      _exportDateBoxDecoration = BoxDecoration(
        border: Border.all(
          color: FTheme.ins.colorScheme.line,
        ),
      );
    }
    return FContainer(
      margin: EdgeInsets.symmetric(
        horizontal: 10,
      ),
      child: FInkWell(
        child: FContainer(
          padding: EdgeInsets.symmetric(
            vertical: 10,
            horizontal: 20,
          ),
          decoration: _exportDateBoxDecoration,
          child: FCenter(child: _titleText),
        ),
        onTap: () {
          setState(() {
            selectExportDate = value;
          });
        },
      ),
    );
  }

  Future<void> exportAppLogZip() async {
    DateFormat format = DateFormat("yyyMMddHHmmss");
    var dateStr = format.format(DateTime.now());
    var fileName = "${i18nBook.common.flyinsonoLog}-$dateStr.zip";
    await rpc.platform.exportAppLogZipFile(selectExportDate.index, fileName);
    controller.state.exportState = ExportStatusType.Downloading;
    router.back();
    router.dialog(
      () => FExportState(),
    );
  }
}

class FExportState extends FStatelessWidget {
  final controller = Get.find<UserSettingController>();
  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      ExportStatusType? exportState = controller.state.exportState;
      if ([
        ExportStatusType.Starting,
        ExportStatusType.Downloading,
        ExportStatusType.Copying,
        ExportStatusType.Packaging
      ].contains(exportState)) {
        //PromptBox.toast(i18nBook.setting.exportingLogs.t);
      } else if (exportState == ExportStatusType.Finished) {
        PromptBox.toast(i18nBook.common.savedeskTokSuccess2.t);
        router.back();
      } else {
        //PromptBox.toast(i18nBook.setting.failedExportLog.t);
        rpc.platform.abortExportOperation();
        router.back();
      }

      return FSizedBox();
    });
  }
}
