import 'package:flutter/material.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/base_define/page.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';
import 'package:fis_theme/theme.dart';

/// 麦克风检测
class MicrophoneDetection extends GetView<HardwareDetectionController>
    implements FPage {
  final bool isdetection;
  MicrophoneDetection({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.detectingMicrophone = false;
    return Row(
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              width: 300,
              height: _subTitleRowHright,
              child: Row(
                children: [
                  Text(i18nBook.setting.mic.t, style: smallTitleStyle),
                  SizedBox(width: 10),
                  InkWell(
                    onTap: controller.refreshMicrophoneList,
                    child: Icon(
                      Icons.refresh,
                      size: 24,
                    ),
                  ),
                  SizedBox(width: 10),
                  Obx(() {
                    if (controller.state.microphoneAvailable == null)
                      return Container();
                    return controller.state.microphoneAvailable!
                        ? CommonWidgets.deviceAvailableTip()
                        : CommonWidgets.deviceUnavailableTip();
                  }),
                ],
              ),
            ),
            FSizedBox(
              height: 13,
            ),
            _microphoneSelector(),
          ],
        ),
        SizedBox(width: 20),
        FColumn(
          children: [
            FSizedBox(
              height: 13,
            ),
            DetectButton(
              deviceType: HardwareDeviceType.microphone,
              subTitleRowHright: _subTitleRowHright,
            ),
          ],
        )
      ],
    );
  }

  /// 麦克风选择器
  Widget _microphoneSelector() {
    return Obx(
      () {
        List<HardwareDevice> source = controller.state.microphoneList;
        bool noMicrophone = source.isEmpty;
        if (controller.state.searchingMicrophone)
          return CommonWidgets.deviceFindingTip();
        return noMicrophone
            ? CommonWidgets.deviceNotFoundTip()
            : FSelect<HardwareDevice, String>(
                source: source,
                height: isdetection ? 35 : 30,
                width: 300,
                value: controller.state.currentMicrophone?.id,
                optionValueExtractor: (value) {
                  return value.id;
                },
                fontFamily: FTheme.ins.localeSetting.fontFamily,
                optionLabelExtractor: (value) {
                  return value.name;
                },
                onSelectChanged: (value, index) {
                  if (value == null) return;
                  controller.selectMicrophoneById(value);
                },
                fontSize: isdetection ? FFontSizes.H4 : FFontSizes.H6,
              );
      },
    );
  }
}
