import 'package:flutter/material.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:get/get.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/values/font_sizes.dart';
import '../index.dart';
import 'widgets.dart';

/// 扬声器检测
class SpeakerDetection extends GetView<HardwareDetectionController>
    implements FPage {
  final bool isdetection;
  SpeakerDetection({Key? key, this.isdetection = false}) : super(key: key);

  @override
  String get pageName => "device_detection";

  late TextStyle smallTitleStyle = TextStyle(
    fontSize: isdetection ? FFontSizes.H4 : FFontSizes.H6,
    height: 1,
  );

  /// 副标题行高：[摄像头]、[麦克风]、[扬声器]
  static const _subTitleRowHright = 30.0;

  @override
  Widget build(BuildContext context) {
    controller.state.detectingSpeaker = false;
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: _subTitleRowHright,
              width: 300,
              child: Row(
                children: [
                  Text(i18nBook.setting.speaker.t, style: smallTitleStyle),
                  SizedBox(width: 10),
                  InkWell(
                    onTap: controller.refreshSpeakerList,
                    child: Icon(
                      Icons.refresh,
                      size: 24,
                    ),
                  ),
                  SizedBox(width: 10),
                  Obx(() {
                    if (controller.state.speakerAvailable == null)
                      return Container();
                    return controller.state.speakerAvailable!
                        ? CommonWidgets.deviceAvailableTip()
                        : CommonWidgets.deviceUnavailableTip();
                  }),
                ],
              ),
            ),
            FSizedBox(
              height: 13,
            ),
            _speakerSelector(),
          ],
        ),
        SizedBox(width: 20),
        FColumn(
          children: [
            FSizedBox(
              height: 13,
            ),
            DetectButton(
              deviceType: HardwareDeviceType.speaker,
              subTitleRowHright: _subTitleRowHright,
            ),
          ],
        )
      ],
    );
  }

  /// 扬声器选择器
  Widget _speakerSelector() {
    return Obx(
      () {
        List<HardwareDevice> source = controller.state.speakerList;
        bool noSpeaker = source.isEmpty;
        if (controller.state.searchingSpeaker)
          return CommonWidgets.deviceFindingTip();
        return noSpeaker
            ? CommonWidgets.deviceNotFoundTip()
            : Container(
                height: 30,
                width: 300,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Tooltip(
                        message: "${source.first.name}",
                        child: Text(
                          source.first.name,
                          style: TextStyle(
                            fontSize:
                                isdetection ? FFontSizes.H4 : FFontSizes.H6,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ],
                ),
              );
      },
    );
  }

  Widget _displayFirstSpeaker(List<HardwareDevice> source) {
    return Container(
      height: 30,
      width: 300,
      child: Row(
        children: [
          Expanded(
            child: Tooltip(
              message: "${source.first.name}",
              child: Center(
                child: Text(
                  source.first.name,
                  style: TextStyle(
                    fontSize: FFontSizes.H6,
                    color: Colors.black,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
