
export 'i18n.dart';
export 'cache.dart';
export 'logger.dart';
export 'request.dart';
export 'user_ext.dart';
export 'size_fit.dart';
export 'net_error.dart';
export 'notification.dart';

import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/services.dart';
import 'package:twong/utils/logger.dart';
import 'package:twong/widgets/photo_view.dart';

import 'cache.dart';
import 'package:twong/utils/i18n.dart';
import 'package:twong/router/base.dart';
import 'package:twong/widgets/dialog.dart';

class Utils {
  static String _channel = "android/back/desktop";

  static void showAlert(BuildContext context, {String title, Function ok, String okText, String noText}) {
    showDialog(
        context: context,
        builder: (context) {
          return CustomDialog(
              content: title,
              callback: (res) {
                ok?.call();
              }
          );
        }
    );
  }

  static void notOpen () {
    BotToast.showText(text: '功能暂未开放!');
  }

  static String formatRMB(dynamic num, { bool show = false }) {
    double price = double.parse(num.toString());
    return "${show ? I18n.$ : ""}${price.toStringAsFixed(2)}";
  }

  static void toLogin() {
    if (Cache.navigator == null) return;
    var context = Cache.navigator.currentState.overlay.context;
    Navigator.pushNamed(context, RouteNames.login);
  }

  static void showPhoto({String image, List<String> images, int index = 0}) {
    if (Cache.navigator == null) return;
    var context = Cache.navigator.currentState.overlay.context;
    PhotoListData data;
    if (images != null) {
      data = PhotoListData(index, images);
    } else {
      data = PhotoListData(index, [image]);
    }

    Navigator.pushNamed(context, RouteNames.picture, arguments: data);
  }

  static Future backDeskTop() async {
    var platform = MethodChannel(_channel);
    try {
      await platform.invokeMethod("back_to_desktop");
    } on PlatformException catch (e) {
      debugPrint(e.toString());
    }
    return Future.value(false);
  }
}