import 'package:chat/chat/ChatPage.dart';
import 'package:chat/chat/coin_bag_info.dart';
import 'package:chat/chat/coin_bag_view.dart';
import 'package:chat/chat/company_server_view.dart';
import 'package:chat/chat/group_chat_view.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/home/InformUser.dart';
import 'package:chat/home/ProfilePage.dart';
import 'package:chat/home/SearchPage.dart';
import 'package:chat/home/audio_chat_view.dart';
import 'package:chat/home/create_group_view.dart';
import 'package:chat/home/forward_view.dart';
import 'package:chat/home/group_all_member.dart';
import 'package:chat/home/group_qr_view.dart';
import 'package:chat/home/homeMain.dart';
import 'package:chat/home/qr_scanner_view.dart';
import 'package:chat/home/qr_view.dart';
import 'package:chat/home/service_view.dart';
import 'package:chat/home/translate_robot.dart';
import 'package:chat/home/webview_page.dart';
import 'package:chat/map/location_result.dart';
import 'package:chat/map/map_view.dart';
import 'package:chat/models/ChatMsg.dart';
import 'package:chat/models/UserInfo.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/utils/NetUtil.dart';
import 'package:chat/utils/conversation_table.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

class AppNavigator {
  static SlideTransition createTransition(
      Animation<double> animation, Widget child) {
    return new SlideTransition(
      position: new Tween<Offset>(
        begin: const Offset(1.0, 0.0),
        end: const Offset(0.0, 0.0),
      ).animate(animation),
      child: child,
    );
  }

  static FadeTransition createFadeTransition(
      Animation<double> animation, Widget child) {
    return FadeTransition(
      opacity: Tween(begin: 1.0, end: 0.0).animate(
          CurvedAnimation(parent: animation, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  }

  static SlideTransition createUpTransition(
      Animation<double> animation, Widget child) {
    return SlideTransition(
      position: new Tween<Offset>(
        begin: const Offset(0.0, 1.0),
        end: const Offset(0.0, 0.0),
      ).animate(animation),
      child: child,
    );
  }

  static defaultPush(BuildContext context, Widget scene) {
    Navigator.of(context).push(
      new MaterialPageRoute(
        builder: (context) {
          return scene;
        },
      ),
    );
  }

  static push(BuildContext context, Widget scene) {
    Navigator.push(context, PageRouteBuilder(pageBuilder: (BuildContext context,
        Animation<double> animation, Animation<double> secondaryAnimation) {
      // 跳转的路由对象
      return createTransition(animation, scene);
    }));
  }

  //主页
  static pushHomePage(BuildContext context) {
    Navigator.pushAndRemoveUntil(context, PageRouteBuilder(pageBuilder:
        (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
      // 跳转的路由对象
      return createTransition(animation, HomeMain());
    }), (route) => route == null);
  }

  //search
  static pushSearchPage(BuildContext context) {
    AppNavigator.push(context, SearchPage());
  }

  //扫描
  static pushScannerPage(BuildContext context) {
    AppNavigator.defaultPush(context, QrScannerPage());
  }

  //聊天
  static pushChatPage(BuildContext context, int friendId,
      {enterType = 0, enterContent}) async {
    if (enterType == 1 ||enterType == 2) {
      Navigator.of(context).pop(context);
    }
    AppNavigator.push(
        context,
        ChatPage(
          key: Key('Chat'),
          friendId: friendId,
          enterType: enterType,
          enterContent: enterContent,
        ));
  }

  //群聊
  static pushGroupChatPage(BuildContext context, GroupInfoModel groupInfoModel,
      {enterType = 0, enterContent}) async {
    if (enterType == 1 ||enterType == 2) {
      Navigator.of(context).pop(context);
    }

    AppNavigator.defaultPush(
        context,
        GroupChatPage(
          key: Key('GroupChat'),
          groupInfoModel: groupInfoModel,
          enterType: enterType,
          enterContent: enterContent,
        ));
  }

  //客服
  static pushServicePage(BuildContext context, {questionIndex = -1}) async {
    AppNavigator.push(context, ServiceCenterPage(questionIndex : questionIndex));
  }

  //群成员列表
  static pushGroupAllMember(BuildContext context, GroupInfoModel model) async {
    AppNavigator.push(context, GroupAllMember(groupInfoModel: model));
  }

  //群聊二维码
  static pushGroupQrPage(BuildContext context, GroupInfoModel model) {
    AppNavigator.push(context, GroupQrPage(model));
  }

  //内部webview
  static pushWebview(BuildContext context, String url) async {
    AppNavigator.push(context, WebviewPage(url));
  }

  //连麦
  static pushAudioChatPage(BuildContext context, UserInfo userInfo,
      [bool isReply = false]) {
    //if (SocketUtil().isConnect && SocketUtil().isLogin) {
    if (NetWork().isConnect && NetWork().isLogin) {
      AppNavigator.push(
          context, AudioChatPage(userInfo: userInfo, isReplay: isReply));
    } else {
      showToast(I18n.of(context).net_error);
    }
  }

  //红包发送界面
  static pushCoinBagPage(context) {
    Navigator.push(context, PageRouteBuilder(pageBuilder: (BuildContext context,
        Animation<double> animation, Animation<double> secondaryAnimation) {
      // 跳转的路由对象
      return createUpTransition(animation, CoinBagPage());
    }));
  }

  //红包打开界面
  static pushCoinBagInfoPage(context, MsgModel msgModel, titleStr) {
    AppNavigator.push(context, CoinBagInfoPage(msgModel, titleStr: titleStr));
  }

  //打开个人信息页面 fromWhere- 0私聊 1首页 2 群聊 addMode : 0不添加，1添加朋友，2新朋友
  static pushProfileInfoPage(BuildContext context, int userId,
      {int fromWhere = 0, int addMode = 0}) {
    AppNavigator.push(
        context,
        ProfilePage(
          userId: userId,
          fromWhere: fromWhere,
          addMode: addMode,
        ));
  }

  //打开地图界面
  static pushMapPage(BuildContext context, LocationResult result) {
    AppNavigator.push(context, MapView(locationResult: result));
  }

  //打开举报界面
  static pushInformUserPage(BuildContext context, bool isMale, int userId) {
    Navigator.of(context).push(
      new MaterialPageRoute(
        builder: (context) {
          return InformUserPage(
            isMan: isMale,
            userId: userId,
          );
        },
      ),
    );
  }

  //我的二维码
  static pushQrPage(BuildContext context) {
    AppNavigator.push(context, QrPage());
  }

  //进入翻译机器人页面
  static pushTranslateRobotPage(BuildContext context) {
    AppNavigator.push(context, TranslateRobotPage());
  }

  //进入反馈小助手页面
  static pushCompanyServerPage(BuildContext context) {
    AppNavigator.push(context, CompanyServerPage());
  }

  //发起群聊界面
  static pushGroupCreatePage(
      BuildContext context, List<FriendModel> friendList) {
    AppNavigator.push(
        context, CreateGroupPage(GroupOperatingPageType.CreateGroup, [], null));
}

  //发起群聊界面
  static pushForwardPage(BuildContext context, MsgModel msg) {
    AppNavigator.push(context, ForwardPage(msg));
  }
}
