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/pages/user/user_device/controller.dart';

///A应用标签
class AiApplication extends FisView<DeviceController> {
  final ScrollController _scrollController = ScrollController();
  @override
  FWidget build(BuildContext context) {
    return FContainer(
        margin: EdgeInsets.all(
          30,
        ),
        child: FObx(() {
          if (controller.state.diagnosisModule.length == 0 ||
              controller.state.terminals.length == 0) {
            return NoDataWidget();
          }
          return FColumn(
            children: [
              _buildOperateTitlesRow(),
              _buildOperate(context),
            ],
          );
        }));
  }

  ///AI应用标签
  _buildOperateTitlesRow() {
    return FRow(
      children: [
        FContainer(
          width: 200,
          child: FCenter(
            child: FText(
              i18nBook.user.aiApplicationName.t,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
          ),
        ),
        FContainer(
          width: 200,
          child: FCenter(
            child: FText(
              i18nBook.user.openState.t,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
          ),
        ),
        FContainer(
          width: 200,
          child: FCenter(
            child: FText(
              i18nBook.common.operation.t,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
          ),
        ),
      ],
    );
  }

  ///AI相关操作
  _buildOperate(BuildContext context) {
    return FObx(
      () {
        final diagnosisModule = controller.state.diagnosisModule;
        return FAlwaysScrollListView(
          scrollController: _scrollController,
          child: QuickFWidget(ScrollConfiguration(
            behavior:
                ScrollConfiguration.of(context).copyWith(scrollbars: false),
            child: FListView.builder(
              shrinkWrap: true,
              controller: _scrollController,
              itemCount: diagnosisModule.length,
              itemBuilder: (BuildContext context, int index) {
                return FContainer(
                  padding: EdgeInsets.symmetric(
                    vertical: 10,
                  ),
                  child: FRow(
                    children: [
                      FContainer(
                        width: 200,
                        child: FCenter(
                          child: FText(
                            controller.getValues(
                                diagnosisModule[index].diagnosisModule!),
                          ),
                        ),
                      ),
                      FContainer(
                        width: 200,
                        child: FCenter(
                          child: FText(
                            diagnosisModule[index].enabled
                                ? i18nBook.user.open.t
                                : i18nBook.user.closed.t,
                            style: diagnosisModule[index].enabled
                                ? TextStyle(
                                    color: Colors.green,
                                  )
                                : TextStyle(
                                    color: Colors.red,
                                  ),
                          ),
                        ),
                      ),
                      FContainer(
                        width: 200,
                        child: FCenter(
                          child: FSwitch(
                            value: diagnosisModule[index].enabled,
                            inactiveThumbColor: FTheme.ins.colorScheme.primary,
                            inactiveTrackColor: Colors.grey.withAlpha(88),
                            activeColor: Colors.white,
                            activeTrackColor: FTheme.ins.colorScheme.primary,
                            onChanged: (value) {
                              controller.modifyDeviceDiagnosisModuleStateAsync(
                                value,
                                diagnosisModule[index].diagnosisModule!,
                              );
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          )),
        );
      },
    );
  }
}
