
export 'base.dart';
export 'user.dart';
export 'order.dart';
export 'animation.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:twong/pages/not_found.dart';

import 'package:twong/utils/index.dart';
import 'package:twong/pages/product/product_comment.dart';
import 'package:twong/router/animation.dart';
import 'package:twong/pages/cate.dart';
import 'package:twong/pages/login/protocol.dart';
import 'package:twong/pages/product/product_details.dart';
import 'package:twong/pages/product/product_list.dart';
import 'package:twong/pages/login/login.dart';
import 'package:twong/pages/login/register.dart';
import 'package:twong/pages/search.dart';
import 'package:twong/pages/tabs.dart';
import 'package:twong/pages/tabs/cart.dart';
import 'package:twong/pages/tabs/user.dart';
import 'package:twong/pages/tabs/wallet.dart';
import 'package:twong/router/base.dart';
import 'package:twong/router/order.dart';
import 'package:twong/router/user.dart';
import 'package:twong/widgets/photo_view.dart';


Map<String, RouterData> routerList = {};

initRouterList() {
  routerList.addAll(list);
  routerList.addAll(UserRouter.list);
  routerList.addAll(OrderRouter.list);
}

Map<String, RouterData> get list {
  return {
    RouteNames.home: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return FadeRoute(Tabs());
        }
    ),
    RouteNames.cate: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return EnterExitRoute(null, CatePage(), false);
        }
    ),
    RouteNames.wallet: RouterData(
        auth: true,
        page: ({BuildContext context, RouteSettings settings}) {
          return MaterialPageRoute(builder: (context) => WalletPage());
        }
    ),
    RouteNames.cart: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return CupertinoPageRoute(builder: (context) => CartPage(show: settings.arguments));
        }
    ),
    RouteNames.user: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return MaterialPageRoute(builder: (context) => UserPage());
        }
    ),
    RouteNames.search: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return FadeRoute(SearchPage());
        }
    ),
    RouteNames.productList: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return EnterExitRoute(null, ProductListPage(settings.arguments), true);
        }
    ),
    RouteNames.productDetails: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return CupertinoPageRoute(builder: (context) => ProductDetailsPage(settings.arguments));
        }
    ),
    RouteNames.login: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return DownUpRoute(null, LoginPage(), true);
        }
    ),
    RouteNames.register: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return DownUpRoute(null, RegisterPage(), true);
        }
    ),
    RouteNames.protocol: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return CupertinoPageRoute(builder: (context) => ProtocolPage(settings.arguments));
        }
    ),

    RouteNames.comment: RouterData(
        auth: true,
        page: ({BuildContext context, RouteSettings settings}) {
          return EnterExitRoute(null, ProductCommentPage(settings.arguments), true);
        }
    ),

    RouteNames.picture: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return CupertinoPageRoute(builder: (context) => PhotoViewPage(settings.arguments));
        }
    ),

    RouteNames.notFound: RouterData(
        auth: false,
        page: ({BuildContext context, RouteSettings settings}) {
          return CupertinoPageRoute(builder: (context) => NotFoundPage());
        }
    ),
  };
}

Route<dynamic> onGenerateRoute(RouteSettings settings) {
  final String routeName = settings.name;
  Log.i("route to ==> $routeName");

  if(!routerList.containsKey(routeName)) {
    return routerList[RouteNames.notFound].page();
  }
  var routeData = routerList[routeName];
  if(routeData?.auth != null && routeData.auth && Cache.token == null){
    Log.i("route: need login");
    BotToast.showText(text:"请先进行登陆!");
    return routerList[RouteNames.login].page(settings: settings);
  }

  return routeData.page(settings: settings);
}
