import 'package:fis_theme/theme.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/services/entity/local_setting.dart';
import 'package:flyinsono/managers/interfaces/flyinsono_college.dart';
import 'package:flyinsono/pages/college/controller.dart';
import 'package:flyinsono/pages/main/home/widgets/dialog_page.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:get/get.dart';

class RecordSettingDialog extends FStatefulWidget {
  const RecordSettingDialog({Key? key});

  @override
  _RecordSettingDialogState createState() => _RecordSettingDialogState();
}

class _RecordSettingDialogState extends FState<RecordSettingDialog> {
  final controller = Get.find<FlyinsonoCollegeController>();
  final flyinsonoCollegeManager = Get.find<IFlyinsonoCollegeManager>();
  String recordFilePath = "";

  @override
  void initState() {
    super.initState();
    recordFilePath = LocalSetting.ins.liveRecordPath ?? "";
    flyinsonoCollegeManager.onRecordFilePathUpdate
        .addListener(_onRecordFilePathUpdate);
  }

  @override
  void dispose() {
    flyinsonoCollegeManager.onRecordFilePathUpdate
        .removeListener(_onRecordFilePathUpdate);
    super.dispose();
  }

  void _onRecordFilePathUpdate(e, String path) {
    setState(() {
      recordFilePath = path;
    });
  }

  @override
  FWidget build(BuildContext context) {
    return FOverlayContainer(
      child: FSimpleDialog(
        contentPadding: EdgeInsets.all(0),
        insetPadding: EdgeInsets.all(10),
        title: FText(
          i18nBook.college.recordSettings.t,
          style: TextStyle(
            color: Colors.white,
            fontSize: 18,
          ),
        ),
        children: [
          Center(
            child: Container(
              width: 400,
              height: 150,
              // color: Colors.red,
              child: Column(
                children: [
                  SizedBox(
                    height: 20,
                  ),
                  buildTitle(),
                  SizedBox(
                    height: 15,
                  ),
                  buildPath(),
                  SizedBox(
                    height: 20,
                  ),
                  buildButton(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  FWidget buildTitle() {
    return FRow(
      children: [
        FText(
          i18nBook.college.recordFileStorageDir.t,
          style: TextStyle(
            color: Colors.black87,
            fontSize: 14,
          ),
        ),
      ],
    );
  }

  FWidget buildPath() {
    TextStyle textStyle = TextStyle(
      color: Colors.grey,
      fontSize: 14,
      fontFamily: FTheme.ins.localeSetting.fontFamily,
      fontFamilyFallback: [
        FTheme.ins.data.localeSettings.chinese.fontFamily,
      ],
    );
    if (recordFilePath.isEmpty) {
      return FRow(
        children: [
          FExpanded(
            child: FText(
              i18nBook.college.defaultSavePath.t,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: textStyle,
            ),
          ),
        ],
      );
    }
    return FRow(
      children: [
        FExpanded(
          child: FInkWell(
            onTap: () {
              controller.openRecordPath();
            },
            child: Padding(
              padding: EdgeInsets.all(2),
              child: FTextTooltip(
                message:
                    i18nBook.college.openStorageDir.t + " " + recordFilePath,
                offset: Offset(0, -10),
                decoration: BoxDecoration(
                  color: Colors.black87,
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: FText(
                  recordFilePath.breakWord(),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: textStyle.copyWith(
                    decoration: TextDecoration.underline,
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget buildButton() {
    return Row(
      children: [
        OutlinedButton(
          onPressed: () {
            controller.openFolderPicker(recordFilePath);
          },
          child: FText(
            i18nBook.college.selectStorageDir.t,
            style: TextStyle(
              color: Colors.black87,
              height: 1,
              fontSize: 13,
            ),
          ),
        ),
      ],
    );
  }
}
