import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/forum.dart';
import 'package:sport/bean/game.dart';
import 'package:sport/bean/post.dart';
import 'package:sport/bean/post_user.dart';
import 'package:sport/bean/rank_game_info.dart';
import 'package:sport/bean/user.dart';
import 'package:sport/pages/social/user_detail_page.dart';

import '../application.dart';
import 'routes.dart';

typedef PageCreate = Future<List> Function(int pageNum);

class NavigatorUtil {
  static Future _navigateTo(BuildContext context, String path,
      {bool replace = false,
      bool clearStack = false,
      Duration transitionDuration = const Duration(milliseconds: 250),
      RouteTransitionsBuilder transitionBuilder}) {
    return Application.router.navigateTo(context, path,
        replace: replace,
        clearStack: clearStack,
        transitionDuration: transitionDuration,
        transitionBuilder: transitionBuilder,
        transition: TransitionType.material);
  }

  static String toJson(Object value) {
    if (value == null) return "";
    return Uri.encodeComponent(json.encode(value));
  }

  static Future goPage(BuildContext context, WidgetBuilder builder, {RouteSettings settings}) {
    return Navigator.push(context, MaterialPageRoute(builder: builder, settings:  settings));
  }

  static Future pushAndRemoveUntil(BuildContext context, WidgetBuilder builder, RouteSettings settings) {
    return Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: builder, settings: settings), (check) {
      if (check.settings.name == "/") return true;
      return false;
    });
  }

  ///跳转 uri: Routes.any
  static Future go(BuildContext context, String uri) {
    return _navigateTo(context, uri);
  }

  /// 首页
  static void goHomePage(BuildContext context) {
    _navigateTo(context, Routes.home, clearStack: true);
  }

  /// social detail
  static void goSocialDetail(BuildContext context, Forum forum) {
    _navigateTo(context, "${Routes.socialDetail}?data=${toJson(forum)}");
  }

  /// social post detail
  static void goSocialPostDetail(BuildContext context, Post post, {bool comment = false}) {
    _navigateTo(context, "${Routes.socialPostDetail}?data=${toJson(post)}&comment=$comment");
  }

  /// social user detail
  static Future goSocialUserDetail(BuildContext context, PostUser user) {
    return _navigateTo(context, "${Routes.socialUserDetail}?data=${toJson(user)}");
  }

  static Future<dynamic> goGameDetails(BuildContext context, {GameInfoData details}) {
    return _navigateTo(context, "${Routes.gameDetails}?details=${toJson(details)}");
  }

  static void goGameHistory(BuildContext context, GameInfoData details) {
    _navigateTo(context, "${Routes.sportHistory}?details=${toJson(details)}");
  }

  static void goRankDetails(BuildContext context, int id, int type) {
    _navigateTo(context, "${Routes.rankDetails}?id=$id&type=$type");
  }

  static void goRankPeopleDetails(BuildContext context, {User details}) {
    _navigateTo(context, "${Routes.rankPeopleDetails}?details=${toJson(details)}");
  }

  static void goLogin(
    BuildContext context,
  {int type = 1}
  ) {
    _navigateTo(context, "${Routes.login}?type=$type", clearStack: true);
  }

  static void goLicense(BuildContext context) {
    _navigateTo(context, "${Routes.license}");
  }

  static void goAuthCode(BuildContext context, {String phone}) {
    _navigateTo(context, "${Routes.authCode}?phone=$phone");
  }

  static void goRegister(BuildContext context) {
    _navigateTo(context, "${Routes.register}");
  }

  static void goBasicInfo(BuildContext context, {String username}) {
    _navigateTo(context, "${Routes.basicInfo}?username=${username?.isNotEmpty == true ? Uri.encodeComponent(username) : ""}");
  }

  static void goFindPassword(BuildContext context) {
    _navigateTo(context, "${Routes.findPassword}");
  }

  static void goAchievementDetails(BuildContext context, {int id, int userId = 0, bool replace = false}) {
    _navigateTo(context, "${Routes.achievementDetail}?id=$id&userId=$userId", replace: replace);
  }

  static void goLevel(BuildContext context) {
    _navigateTo(context, Routes.level);
  }
// goback
//  static void goBack(){
//    _navigateTo.pop(context);
//  }

}
