import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:redux/redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import './Localizations.dart';
import 'model/UserInfo.dart';
import 'net/HttpManager.dart';
import 'net/Result.dart';
import 'pages/HomePage.dart';
import 'pages/loginFirst.dart';
import 'redux/AppState.dart';

class MobileCyberGamesApp extends StatelessWidget {
  const MobileCyberGamesApp(this.store);

  final Store<AppState> store;

  @override
  Widget build(BuildContext context) {
    return StoreProvider<AppState>(
      store: store,
      child: MaterialApp(
        title: '龙电竞',
        supportedLocales: [
          const Locale('en'), // English
          const Locale('zh'), // China
        ],
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          ChineseCupertinoLocalizations.delegate,
        ],
        theme: ThemeData(
            cardColor: Color(0xFF2B2B42),
            backgroundColor: Color(0xFF222335),
            primaryColor: Color(0xFFD4504B),
            buttonColor: Color(0xFFD4504B),
            accentColor: Color(0xFFD4504B),
            textSelectionColor: Colors.white,
            textTheme: TextTheme(
              subhead: TextStyle(color: Colors.white),
              title: TextStyle(color: Colors.white),
            ),
            primaryTextTheme: TextTheme(
              title: TextStyle(color: Colors.white),
            ),
            primaryIconTheme: IconThemeData(color: Colors.white),
            appBarTheme: AppBarTheme(color: Color(0xFF23253C)),
            buttonTheme: ButtonThemeData(
              buttonColor: Color(0xFFD4504B),
              highlightColor: Color(0xFF8E3B36),
              splashColor: Color(0xFF8E3B36),
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(0))),
            )),
        home: store.state.userInfo != null ? HomePage() : LoginFirst(),
        // home: LoginFirst(),
        routes: {'/home': (BuildContext context) => HomePage()},
        color: Color(0xFF2E3049),
      ),
    );
  }
}

Future<void> main() async {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
    statusBarColor: Colors.transparent,
  ));
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  final JPush jpush = JPush();
  jpush.setup(
    appKey: '52209c854c4d7bfc14c0e60b',
    channel: 'theChannel',
    production: true,
    debug: true,
  );
  jpush.applyPushAuthority(NotificationSettingsIOS(sound: true, alert: true, badge: true));
  final prefs = await SharedPreferences.getInstance();
  HttpManager.token = prefs.getString('token') ?? '';
  Result result = await HttpManager.get('userInfo/getUserInfo');
  AppState state = AppState.empty();
  if (result.success && result.data != null) {
    UserInfo userInfo = UserInfo.fromJson(result.data);
    state.userInfo = userInfo;
    jpush.setAlias(userInfo.id.toString()).then((map) {});
  }
  final Store<AppState> store = Store<AppState>(appReducer, initialState: state);

  FlutterBugly.init(androidAppId: 'bd9a511cb9', iOSAppId: '');

  FlutterBugly.postCatchedException(() {
    runApp(MobileCyberGamesApp(store));
  });
}
