import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:marking_app/common/model/enum/KeyboardType.dart';

class UseScreen {
  ValueNotifier<ScreenDirection> orientationState; // 当前屏幕方向

  UseScreen._({required this.orientationState});

  factory UseScreen.use(BuildContext context) {
    ScreenDirection theOrientation = MediaQuery.of(context).orientation == Orientation.landscape
        ? ScreenDirection.HORIZONTAL_SCREEN
        : ScreenDirection.VERTICAL_SCREEN;

    return UseScreen._(orientationState: useState(theOrientation));
  }

  init(ScreenDirection direction) {
    SystemChrome.setPreferredOrientations(direction != ScreenDirection.VERTICAL_SCREEN
        ? [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]
        : [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
    orientationState.value = direction;
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  }

  dis() {
    ScreenDirection orientation = orientationState.value;
    if (orientation == ScreenDirection.HORIZONTAL_SCREEN) {
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); // 强制竖屏
    }
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
  }

  switchDirection() {
    ScreenDirection orientation = orientationState.value;
    if (orientation == ScreenDirection.HORIZONTAL_SCREEN) {
      // 目前是横屏，切换竖屏
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
    } else {
      SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    }

    orientationState.value = orientation == ScreenDirection.HORIZONTAL_SCREEN
        ? ScreenDirection.VERTICAL_SCREEN
        : ScreenDirection.HORIZONTAL_SCREEN;
  }
}
