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';

///魔盒检测缺少翻译
class SonopostDetectionResult extends FStatefulWidget implements FPage {
  final OnButtonPressedCallback onButtonPressed;
  SonopostDetectionResult({required this.onButtonPressed});
  @override
  String get pageName => 'SonopostDetectionResult';
  @override
  _SonopostDetectionResult createState() => _SonopostDetectionResult();
}

typedef OnButtonPressedCallback = void Function();

class _SonopostDetectionResult extends FState<SonopostDetectionResult> {
  final List<String> _imageUrls = [
    'assets/images/CameraErrorTip.png',
    'assets/images/TerminalErrorTip4.png',
    'assets/images/TerminalErrorTip3.png'
  ];

  bool isShowImage = false;

  int _currentImageIndex = 0;

  void _changeImage(int index) {
    setState(() {
      _currentImageIndex = index;
      isShowImage = true;
    });
  }

  @override
  FWidget build(BuildContext context) {
    return FExpanded(
      child: FColumn(
        children: [
          FContainer(
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                FText(
                  isShowImage
                      ? _getDescription(_currentImageIndex)
                      : i18nBook.setting.termianlTestTip.t,
                  style: TextStyle(
                    fontSize: FFontSizes.H4,
                    fontWeight: FontWeight.bold,
                  ),
                  softWrap: true,
                ),
                FSizedBox(
                  height: 20.0,
                ),
                if (isShowImage)
                  FColumn(
                    children: [
                      if (_currentImageIndex != 3)
                        _buildCustomElevatedButton(
                            name: i18nBook.setting.recheck.t,
                            onPressed: () {
                              setState(() {
                                _currentImageIndex = 0;
                                isShowImage = false;
                                widget.onButtonPressed();
                              });
                            }),
                      FSizedBox(
                        height: 15.0,
                      ),
                      if (_currentImageIndex == 2)
                        FContainer(
                          child: FCenter(
                              child: FImage.asset(
                            'assets/images/TerminalErrorTip1.png',
                          )),
                        ),
                      if (_currentImageIndex != 3)
                        FContainer(
                          child: FCenter(child: _getTipImage()),
                        ),
                    ],
                  ),
                if (!isShowImage)
                  FRow(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      FSizedBox(
                        width: 15.0,
                      ),
                      _buildCustomElevatedButton(
                          name: i18nBook.setting.ultrasoundImageNotVisible.t,
                          onPressed: () {
                            _changeImage(0);
                          }),
                      FSizedBox(
                        width: 15.0,
                      ),
                      _buildCustomElevatedButton(
                          name: i18nBook.setting.ultrasoundImageIsVisible.t,
                          onPressed: () {
                            _changeImage(3);
                          }),
                      FSizedBox(
                        height: 15.0,
                      ),
                    ],
                  ),
              ],
            ),
          )
        ],
      ),
    );
  }

  ///获取结果描述
  String _getDescription(int currentIndex) {
    switch (currentIndex) {
      case 0:
        return i18nBook.setting.nothingInTerminalTip.t;
      default:
        return i18nBook.setting.ultrasoundStatusNormal.t;
    }
  }

  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),
      ),
    );
  }

  _getTipImage() {
    var image = FImage.asset(
      _imageUrls[_currentImageIndex],
      width: 700,
      height: 500,
    );

    return image;
  }
}
