import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/desktop/names.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/views/desktop/hardware_detection/widgets/sonopost_detection_result.dart';
import 'package:flyinsono/views/desktop/hardware_detection/widgets/speaker_volume_setting.dart';
import 'package:get/get.dart';
import '../controller.dart';
import '../state.dart';
import 'camera_detection.dart';
import 'sonopost_detection.dart';

import 'camera_ui.dart';
import 'microphone_detection.dart';
import 'microphone_ui.dart';
import 'speaker_detection.dart';
import 'speaker_ui.dart';
import 'package:fis_theme/theme.dart';

///Todo:finlay 检测助手导航页面未完成
class DetectionAssistantNavigation extends FStatefulWidget implements FPage {
  final bigTitleStyle = TextStyle(
    fontSize: FFontSizes.H4,
    fontWeight: FontWeight.bold,
  );
  @override
  _DetectionAssistantNavigation createState() =>
      _DetectionAssistantNavigation();

  @override
  String get pageName => 'DetectionAssistantNavigation';
}

class _DetectionAssistantNavigation
    extends FState<DetectionAssistantNavigation> {
  final controller = Get.find<HardwareDetectionController>();
  int _stepIndex = 0;
  // final List<String> _navTitles = [
  //   i18nBook.setting.start.t,
  //   i18nBook.setting.cameraDetection.t,
  //   i18nBook.setting.speakerDetection.t,
  //   i18nBook.setting.microphoneDetection.t,
  //   // i18nBook.setting.sonopostDetetion.t,
  // ];
  bool isShowNext = false;

  @override
  void initState() {
    super.initState();
    // WidgetsBinding.instance.addPostFrameCallback((call) {
    //   if (!controller.state.isOldVBox) {
    //     _navTitles.add(i18nBook.setting.sonopostDetetion.t);
    //     setState(() {});
    //   }
    // });
  }

  @override
  FWidget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var colorScheme = FTheme.ins.colorScheme;
    return FContainer(
      width: size.width,
      height: 800,
      child: FColumn(
        children: [
          FContainer(
            height: 80,
            color: colorScheme.header,
            child: FObx(
              () => FRow(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: List.generate(
                  controller.state.navTitles.length,
                  (index) => FGestureDetector(
                    name: '',
                    businessParent: this.widget,
                    child: FContainer(
                      alignment: Alignment.center,
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      decoration: BoxDecoration(
                        color: _stepIndex == index
                            ? Colors.white.withOpacity(0.2)
                            : Colors.transparent,
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: FText(
                        controller.state.navTitles[index],
                        style: TextStyle(
                          fontSize: FFontSizes.H4,
                          fontWeight: FontWeight.bold,
                          color: //_stepIndex == index
                              Colors.white,
                          // : Colors.grey[400],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
          FExpanded(
            child: FRow(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (_stepIndex == 0) ...[
                  FExpanded(
                    flex: 2,
                    child: FContainer(
                      child: FCenter(
                          child: FImage.asset(
                        'assets/images/CartSystem.png',
                      )),
                    ),
                  ),
                  FExpanded(
                    flex: 3,
                    child: FContainer(
                      padding: EdgeInsets.only(left: 20, bottom: 200),
                      child: FCenter(
                        child: FColumn(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            FSizedBox(height: 120),
                            FText(
                              i18nBook.setting.welcomeToFlyinsono.t,
                              softWrap: true,
                              style: TextStyle(
                                  fontSize: 24, fontWeight: FontWeight.bold),
                            ),
                            FSizedBox(height: 20),
                            FText(
                              i18nBook.setting.welcomeTipOne.t,
                              softWrap: true,
                              style: TextStyle(fontSize: 24),
                            ),
                            FText(
                              i18nBook.setting.welcomeTipTwo.t,
                              softWrap: true,
                              style: TextStyle(fontSize: 24),
                            )
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
                if (_stepIndex == 1)
                  FContainer(
                    height: 800,
                    width: 800,
                    padding: EdgeInsets.fromLTRB(260, 300, 0, 0),
                    child: FCenter(
                      child: FColumn(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _buildStep(
                              CameraDetection(
                                isdetection: true,
                              ),
                              CameraUI()),
                          FSizedBox(
                            height: 15,
                          ),
                          FObx(() {
                            return _buildResult(HardwareDeviceType.camera,
                                controller.state.cameraAvailable);
                          }),
                        ],
                      ),
                    ),
                  ),
                if (_stepIndex == 2)
                  FContainer(
                    height: 800,
                    width: 800,
                    padding: EdgeInsets.fromLTRB(260, 300, 0, 0),
                    child: FCenter(
                      child: FColumn(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _buildStep(
                            SpeakerDetection(
                              isdetection: true,
                            ),
                            SpeakerUI(
                              fontsize: FFontSizes.H4,
                            ),
                            middleUI: QuickFWidget(SpeakerVolumeSetting(
                              fromAssistant: true,
                            )),
                          ),
                          FSizedBox(
                            height: 15,
                          ),
                          FObx(() {
                            return _buildResult(HardwareDeviceType.speaker,
                                controller.state.speakerAvailable);
                          }),
                        ],
                      ),
                    ),
                  ),
                if (_stepIndex == 3)
                  FContainer(
                    height: 800,
                    width: 800,
                    padding: EdgeInsets.fromLTRB(260, 300, 0, 0),
                    child: FCenter(
                      child: FColumn(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _buildStep(
                              MicrophoneDetection(
                                isdetection: true,
                              ),
                              MicrophoneUI(
                                fontSize: FFontSizes.H4,
                              )),
                          FSizedBox(
                            height: 15,
                          ),
                          FObx(() {
                            return _buildResult(HardwareDeviceType.microphone,
                                controller.state.microphoneAvailable);
                          }),
                        ],
                      ),
                    ),
                  ),
                if (_stepIndex == 4)
                  FRow(
                    children: [
                      FContainer(
                        child: FColumn(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            FSizedBox(height: 20),
                            FExpanded(
                              child: SonopostDetaction(isdetection: true),
                            ),
                            FSizedBox(height: 20),
                          ],
                        ),
                      ),
                    ],
                  ),
                if (_stepIndex == 4)
                  FContainer(
                    width: 860,
                    child: FColumn(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        FSizedBox(height: 15),
                        SonopostDetectionResult(
                          onButtonPressed: () {
                            setState(() {
                              _stepIndex = 4;
                            });
                          },
                        ),
                      ],
                    ),
                  ),
              ],
            ),
          ),
          FContainer(
            height: 80,
            child: FButtonGroup(children: [
              if (_stepIndex == 0)
                _buildCustomElevatedButton(
                    name: i18nBook.setting.skip.t,
                    onPressed: () async {
                      router.offAll(RouteNames.Home.Home);
                    }),
              if (_stepIndex != 0)
                _buildCustomElevatedButton(
                    name: i18nBook.remoteControl.previousStep.t,
                    onPressed: () {
                      controller.stopDetectingSpeaker();
                      setState(() {
                        _stepIndex--;
                      });
                    }),
              if (_stepIndex != 4 &&
                  !(_stepIndex == 3 && controller.state.isOldVBox))
                _buildCustomElevatedButton(
                    name: i18nBook.remoteControl.nextStep.t,
                    onPressed: () {
                      controller.stopDetectingSpeaker();
                      setState(() {
                        _stepIndex++;
                      });
                    }),
              if (_stepIndex == 4 ||
                  (_stepIndex == 3 && controller.state.isOldVBox))
                _buildCustomElevatedButton(
                    name: i18nBook.common.finish.t,
                    onPressed: () async {
                      router.offAll(RouteNames.Home.Home);
                    }),
            ]),
          ),
        ],
      ),
    );
  }

  ///build step
  FWidget _buildStep(FWidget detection, Widget ui, {FWidget? middleUI}) {
    return FCenter(
        child: FColumn(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FSizedBox(height: 20),
        detection,
        FSizedBox(height: 20),
        middleUI == null ? FContainer() : middleUI,
        middleUI == null ? FContainer() : FSizedBox(height: 20),
        QuickFWidget(ui),
      ],
    ));
  }

  ///build结果
  FWidget _buildResult(HardwareDeviceType deviceType, bool? isDeviceAvailable) {
    if (isDeviceAvailable == null) return FContainer();
    final isAvailable = isDeviceAvailable == true;
    return FColumn(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        FText(
          _getDescriptionTitle(deviceType, isAvailable),
          style: TextStyle(
            fontSize: FFontSizes.H4,
            fontWeight: FontWeight.bold,
          ),
        ),
        FSizedBox(
          height: 15,
        ),
        FText(
          _getDescription(deviceType, isAvailable),
          style: TextStyle(
            fontSize: FFontSizes.H4,
          ),
        ),
      ],
    );
  }

  ///获取结果描述title
  String _getDescriptionTitle(HardwareDeviceType deviceType, bool isAvailable) {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return isAvailable
            ? i18nBook.setting.cameraTestSuccessTip.t
            : i18nBook.setting.cameraTestErrorTip.t;
      case HardwareDeviceType.speaker:
        return isAvailable
            ? i18nBook.setting.speakerTestSuccessTip.t
            : i18nBook.setting.speakerTestErrorTip.t;
      case HardwareDeviceType.microphone:
        return isAvailable
            ? i18nBook.setting.micphoneTestSuccessTip.t
            : i18nBook.setting.micphoneTestErrorTip.t;
      default:
        return '';
    }
  }

  ///获取结果描述
  String _getDescription(HardwareDeviceType deviceType, bool isAvailable) {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return isAvailable
            ? i18nBook.setting.nextTest.t
            : i18nBook.setting.fixCameraTip.t;
      case HardwareDeviceType.speaker:
        return isAvailable
            ? i18nBook.setting.nextTest.t
            : i18nBook.setting.fixSpeakerTip.t;
      case HardwareDeviceType.microphone:
        return isAvailable
            ? i18nBook.setting.nextTest.t
            : i18nBook.setting.fixMicphoneTip.t;
      default:
        return '';
    }
  }

  FWidget _buildCustomElevatedButton({
    required String name,
    required VoidCallback onPressed,
  }) {
    return FElevatedButton(
      businessParent: this.widget,
      name: name,
      onPressed: onPressed,
      child: FText(
        name,
        style: TextStyle(fontSize: 20, color: Colors.white),
      ),
    );
  }
}
