import 'dart:convert';
import 'package:fis_i18n/i18n.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/managers/interfaces/vboxcontrol.dart';
import 'package:get/get.dart';
import 'network_config_card.dart';

class NetworkSettingsDialog extends FStatefulWidget implements FPage {
  final NetworkResult? networkResult;
  NetworkSettingsDialog({
    required this.networkResult,
  });
  @override
  String get pageName => 'NetworkSettingsDialog';

  @override
  FState<NetworkSettingsDialog> createState() => _NetworkSettingsDialogState();
}

class _NetworkSettingsDialogState extends FState<NetworkSettingsDialog> {
  var vBoxControl = Get.find<IVBoxControlManager>();
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      contentPadding: EdgeInsets.symmetric(vertical: 25, horizontal: 100),
      title: FText(
        i18nBook.setting.wiredNetworkConnection.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm4Btn.t,
      onClose: () {
        router.back();
      },
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
          width: 650,
          height: 700,
          child: FColumn(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              NetworkConfigCards(cardInfos: widget.networkResult!.interfaces),
              FSizedBox(
                height: 20,
              ),
              _buildCommonButton(i18nBook.common.save.t, _onSave)
            ],
          ),
        )
      ],
    );
  }

  FWidget _buildCommonButton(String buttonName, VoidCallback onPressed) {
    return FContainer(
      width: 150,
      margin: EdgeInsets.symmetric(
        horizontal: 15,
      ),
      child: FElevatedButton(
        child: FText(buttonName),
        onPressed: () {
          onPressed.call();
        },
        businessParent: this.widget,
        name: buttonName,
        style: ElevatedButton.styleFrom(
          elevation: 0,
          padding: EdgeInsets.symmetric(
            vertical: 15,
            horizontal: 25,
          ),
        ),
      ),
    );
  }

  ///保存网络配置
  void _onSave() async {
    await vBoxControl
        .saveNetworkCardsInfo(json.encode(widget.networkResult!.toJson()));
    router.back();
  }
}
