import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/managers/interfaces/vboxcontrol.dart';

class NetworkConfigCards extends StatefulWidget implements FWidget {
  final List<Interface> cardInfos;
  const NetworkConfigCards({required this.cardInfos});
  @override
  _NetworkConfigCardsState createState() => _NetworkConfigCardsState();
}

class _NetworkConfigCardsState extends State<NetworkConfigCards> {
  List<Interface>? cardInfos;
  @override
  void initState() {
    super.initState();
    cardInfos = widget.cardInfos;
  }

  @override
  Widget build(BuildContext context) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: cardInfos!.map<FWidget>((e) {
        return FColumn(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            FText(
              e.name,
              style: TextStyle(
                fontSize: FFontSizes.H3,
              ),
            ),
            FRow(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                FContainer(
                  width: 250.0,
                  child: FText(i18nBook.setting.enableDHCP.t),
                ),
                FSizedBox(
                  width: 30,
                ),
                FSwitch(
                    value: e.enableDHCP,
                    onChanged: (value) {
                      setState(() {
                        e.enableDHCP = value;
                      });
                    }),
              ],
            ),
            buildInputItem(
                i18nBook.setting.ipAddress.t, e.ipAddress, !e.enableDHCP,
                (value) {
              e.ipAddress = value;
            }),
            buildInputItem(
                i18nBook.setting.subnetMask.t, e.subnetMask, !e.enableDHCP,
                (value) {
              e.subnetMask = value;
            }),
            buildInputItem(
                i18nBook.setting.defaultGateway.t, e.gateway, !e.enableDHCP,
                (value) {
              e.gateway = value;
            }),
            buildInputItem(
                i18nBook.setting.preferredDNS.t, e.preferredDNS, !e.enableDHCP,
                (value) {
              e.preferredDNS = value;
            }),
            buildInputItem(
                i18nBook.setting.standbyDNS.t, e.standbyDNS, !e.enableDHCP,
                (value) {
              e.standbyDNS = value;
            }),
            buildInputItem(i18nBook.setting.macAddress.t, e.macAddress, false,
                (value) {
              e.macAddress = value;
            }),
            FSizedBox(
              height: 20,
            ),
          ],
        );
      }).toList(),
    );
  }

  FWidget buildInputItem(String typeName, String itemValue, bool enableEdit,
      Function(String) onPressed) {
    TextEditingController _controller = TextEditingController(text: itemValue);
    return FRow(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        FContainer(
          width: 150.0,
          child: FText(
            typeName,
            style: TextStyle(
              fontSize: FFontSizes.H6,
            ),
          ),
        ),
        FSizedBox(
          width: 30,
        ),
        FBorderInput(
          controller: _controller,
          height: 36,
          enabled: enableEdit,
          onChanged: (value) {
            onPressed.call(value);
          },
        ),
      ],
    );
  }
}
