/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-15 11:21:21
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-09-27 17:02:25
 * @FilePath: \marking_app\lib\utils\index.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-15 11:21:21
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-18 11:16:22
 * @FilePath: \marking_app\lib\utils\index.dart
 * @Description: 工具页面
 */
library utils;

import 'dart:convert';
import 'dart:math';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/config/request_config.dart';

export "./colorUtils.dart";
export "./fast_data.dart";
export "./common_utils.dart";
export './toast_utils.dart';
export './my_future_builder.dart';
export './dashed_line.dart';
export './anti_shake_throttling.dart';

typedef TheVoidCallback = void Function(VoidCallback);

Future<dynamic> setTimeOut(int milliseconds, call) => Future.delayed(Duration(milliseconds: milliseconds), call);

bool isInteger(num value) => (value % 1) == 0;

void toUpState(TheVoidCallback setState, VoidCallback fn, bool mounted) {
  if (mounted) setState(fn);
}

/// 单纯的Json格式输出打印
void printJson(Object object) {
  try {
    JsonEncoder encoder = const JsonEncoder.withIndent('  ');
    var encoderString = encoder.convert(object);
    // print(encoderString);
    // 不使用print()方法是因为这是单条输出，如果过长无法显示全
    // 所以使用debugPrint()
    debugPrint(encoderString);
    // 下面这语句的效果与debugPrint 相同
    //encoderString.split('\n').forEach((element) => print(element));
  } catch (e) {
    toPrint(val: e);
  }
}

String getDoubleRemoveZero(double? val, [String? defaultVal]) {
  try {
    if (val == null) throw Exception('数据为空');
    List<String> _valArr = val.toString().split('.');
    if (_valArr.length >= 2) {
      if (int.parse(_valArr[1]) == 0) {
        return val.toInt().toString();
      }
      return val.toString();
    }
    return val.toInt().toString();
  } catch (e) {
    return defaultVal ?? '';
  }
}

/// 获取指定范围的随机数
int getRandomNumbers(int minNumber, int maxNumber) {
  int next(int min, int max) => min + Random().nextInt(max - min);
  return next(minNumber, maxNumber);
}

/// 获取页面随机跳转
TransitionType getTransition() {
  try {
    List<TransitionType> transitions = TransitionType.values;
    int theRandomNumber = getRandomNumbers(0, transitions.length - 1);

    TransitionType val = transitions[theRandomNumber];
    if (val == TransitionType.custom) {
      val = getTransition();
    }
    return val;
  } catch (e) {
    return getTransition();
  }
}

/// 去除小数点
String doubleToStringAsFixed(double val, {int fractionDigits = 2}) {
  return val.toStringAsFixed(fractionDigits).replaceAll(RegExp(r'\.0*$'), '');
}

void toPrint({required dynamic val, bool toPrintJson = false}) {
  bool printSwitch = RequestConfig.printSwitch;
  if (printSwitch && val != null) toPrintJson ? printJson(val) : print(val);
}

// 是否是平板
bool isPad([double mobilePhoneScale = 1.2]) {
  return ScreenUtil().scaleWidth > mobilePhoneScale;
}

class EUMNoScrollBehavior extends ScrollBehavior {
  @override
  Widget buildViewportChrome(BuildContext context, Widget child, AxisDirection axisDirection) {
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
        return child;
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
        return GlowingOverscrollIndicator(
          child: child,
          // 不显示头部水波纹
          showLeading: false,
          // 不显示尾部水波纹
          showTrailing: false,
          axisDirection: axisDirection,
          color: Theme.of(context).colorScheme.secondary,
        );
      case TargetPlatform.linux:
        break;
      case TargetPlatform.macOS:
        break;
      case TargetPlatform.windows:
        break;
    }
    return child;
  }
}
