import 'dart:io';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:yumi/modules/chat/chat_route.dart';
import 'package:yumi/modules/auth/login_route.dart';
import 'package:yumi/app/routes/sc_lk_application.dart';

/// 安卓页面切换时长
const Duration kAndroidTransitionDuration = Duration(milliseconds: 375);

/// 进入房间页动画时长
const Duration kOpenRoomTransitionDuration = Duration(milliseconds: 325);


/// 进入房间页过渡动画
SlideTransition kOpenRoomTransitionBuilder( BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,) {
  return SlideTransition(
    position: Tween<Offset>(
      begin: const Offset(0.0, 1.0),
      end: const Offset(0.0, 0.0),
    ).animate(CurvedAnimation(parent: animation, curve: Curves.ease)),
    child: child,
  );
}

/// fluro的路由跳转工具类
class SCNavigatorUtils {
  static bool inChatPage = false;
  static bool inLoginPage = false;
  //不需要页面返回值的跳转
  static Future push(BuildContext context, String path,
      {bool replace = false,
        bool clearStack = false,
        TransitionType? transition,
        Duration? transitionDuration,
        RouteTransitionsBuilder? transitionBuilder}) async {
    FocusScope.of(context).unfocus();
    inLoginPage = path.startsWith("${LoginRouter.login}");
    inChatPage = path.startsWith("${SCChatRouter.chat}");
    final result = await SCLkApplication.router.navigateTo(context, path,
        replace: replace,
        clearStack: clearStack,
        ///在系统语言为ar语会有问题
        transition: (Platform.isAndroid
                ? null
                : TransitionType.cupertino),
        // transitionDuration: transitionDuration ??
        //     (Platform.isAndroid ? kAndroidTransitionDuration : null),
        // transitionBuilder: transitionBuilder ??
        //     (Platform.isAndroid ? (context, animation, secondaryAnimation, child) {
        //       return ScaleTransition(
        //         scale: Tween(begin: 1.0,end: 0.9).animate(CurvedAnimation(parent: secondaryAnimation, curve: Curves.ease)),
        //         child: SlideTransition(
        //           position: Tween<Offset>(
        //             begin: const Offset(1.0, 0.0),
        //             end: const Offset(0.0, 0.0),
        //           ).animate(CurvedAnimation(parent: animation, curve: Curves.ease)),
        //           child: child,
        //         ),
        //       );
        //     } : null)
    );

    return result;
  }

  //需要页面返回值的跳转
  static pushResult(
      BuildContext context, String path, Function(Object) function,
      {bool replace = false,
        bool clearStack = false,
        TransitionType? transition}) {
    FocusScope.of(context).unfocus();
    inLoginPage = path == LoginRouter.login;
    inChatPage = path.startsWith("${SCChatRouter.chat}");
    SCLkApplication.router
        .navigateTo(context, path,
        replace: replace,
        clearStack: clearStack,
        // transition: transition ??
        //     (Platform.isAndroid
        //         ? TransitionType.custom
        //         : TransitionType.cupertino),
        // transitionDuration:
        // Platform.isAndroid ? kAndroidTransitionDuration : null,
        // transitionBuilder:
        // Platform.isAndroid ? (context, animation, secondaryAnimation, child) {
        //   return ScaleTransition(
        //     scale: Tween(begin: 1.0,end: 0.9).animate(CurvedAnimation(parent: secondaryAnimation, curve: Curves.ease)),
        //     child: SlideTransition(
        //       position: Tween<Offset>(
        //         begin: const Offset(1.0, 0.0),
        //         end: const Offset(0.0, 0.0),
        //       ).animate(CurvedAnimation(parent: animation, curve: Curves.ease)),
        //       child: child,
        //     ),
        //   );
        // } : null
    )
        .then((result) {
      // 页面返回result为null
      if (result == null) {
        return;
      }
      function(result);
    }).catchError((error) {
      print('$error');
    });
  }

  /// 直接返回
  static void goBack(BuildContext context) {
    if(inChatPage){
      inChatPage = false;
    }
    FocusScope.of(context).unfocus();
    Navigator.pop(context);
  }

  /// 带参数返回
  static void goBackWithParams(BuildContext context, result) {
    if(inChatPage){
      inChatPage = false;
    }
    FocusScope.of(context).unfocus();
    Navigator.pop(context, result);
  }

  static void popUntil(BuildContext context, RoutePredicate predicate) {
    if(inChatPage){
      inChatPage = false;
    }
    FocusScope.of(context).unfocus();
    Navigator.popUntil(context, predicate);
  }
}
