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 '../index.dart';

/// 设备检测按钮
class DetectButton extends GetView<HardwareDetectionController>
    implements FPage {
  DetectButton({
    Key? key,
    required this.deviceType,
    this.subTitleRowHright = 30.0,
  }) : super(key: key);

  @override
  String get pageName => "detect_button";

  final HardwareDeviceType deviceType;

  /// 副标题行高：[是否能看到画面变化]
  final double subTitleRowHright;

  /// 检测按钮文案
  String get detectButtonText {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return i18nBook.setting.cameraTest.t;
      case HardwareDeviceType.speaker:
        return i18nBook.setting.speakerTest.t;
      case HardwareDeviceType.microphone:
        return i18nBook.setting.micTest.t;
      default:
        return "";
    }
  }

  /// 检测中询问文案
  String get detectingAskText {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return i18nBook.setting.seeImage.t;
      case HardwareDeviceType.speaker:
        return i18nBook.setting.hearMusic.t;
      case HardwareDeviceType.microphone:
        return i18nBook.setting.seeVolumeBar.t;
      default:
        return "";
    }
  }

  List<HardwareDevice> get deviceList {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return controller.state.cameraList;
      case HardwareDeviceType.speaker:
        return controller.state.speakerList;
      case HardwareDeviceType.microphone:
        return controller.state.microphoneList;
      default:
        return [];
    }
  }

  bool get detectingDevice {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return controller.state.detectingCamera;
      case HardwareDeviceType.speaker:
        return controller.state.detectingSpeaker;
      case HardwareDeviceType.microphone:
        return controller.state.detectingMicrophone;
      default:
        return false;
    }
  }

  Function get setDeviceAvailable {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return controller.setCameraAvailable;
      case HardwareDeviceType.speaker:
        return controller.setSpeakerAvailable;
      case HardwareDeviceType.microphone:
        return controller.setMicrophoneAvailable;
      default:
        return () {};
    }
  }

  Function get startDetectDevice {
    switch (deviceType) {
      case HardwareDeviceType.camera:
        return controller.startDetectingCamera;
      case HardwareDeviceType.speaker:
        return controller.startDetectingSpeaker;
      case HardwareDeviceType.microphone:
        return controller.startDetectingMicrophone;
      default:
        return () {};
    }
  }

  @override
  Widget build(BuildContext context) {
    final buttonStyle = ButtonStyle(
      minimumSize: MaterialStateProperty.all<Size>(Size(90, 30)),
      padding: MaterialStateProperty.all(
        EdgeInsets.symmetric(
          vertical: 10,
          horizontal: 8,
        ),
      ),
    );
    return Obx(
      () {
        bool noDevice = deviceList.isEmpty;
        bool isDetectingDevice = detectingDevice;
        if (noDevice) return Container();
        if (isDetectingDevice) {
          return FColumn(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              FContainer(
                height: subTitleRowHright,
                child: FCenter(
                  child: FText(
                    detectingAskText,
                    style: TextStyle(fontSize: 14, height: 1),
                  ),
                ),
              ),
              FRow(
                children: [
                  FInkWell(
                    child: Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 15, vertical: 5),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.green, width: 2),
                        borderRadius: BorderRadius.circular(5),
                      ),
                      child: FText(
                        i18nBook.common.yes.t,
                        style: TextStyle(
                            fontSize: 14, color: Colors.green, height: 1),
                      ),
                    ),
                    onTap: () {
                      setDeviceAvailable(true);
                    },
                  ),
                  FSizedBox(
                    width: 5,
                  ),
                  FInkWell(
                    child: Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 15, vertical: 5),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.red, width: 2),
                        borderRadius: BorderRadius.circular(5),
                      ),
                      child: FText(
                        i18nBook.common.no.t,
                        style: TextStyle(
                            fontSize: 14, color: Colors.red, height: 1),
                      ),
                    ),
                    onTap: () {
                      setDeviceAvailable(false);
                    },
                  ),
                ],
              ),
            ],
          );
        } else {
          return FColumn(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              FSizedBox(
                height: subTitleRowHright,
              ),
              FElevatedButton(
                child: FText(
                  detectButtonText,
                  style: TextStyle(fontSize: 18),
                ),
                style: buttonStyle,
                onPressed: () {
                  startDetectDevice();
                },
                businessParent: this,
                name: "TestDeviceBtn",
              ),
            ],
          );
        }
      },
    );
  }
}
