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 './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(0xFFC2524D),
            buttonColor: Color(0xFFC2524D),
            accentColor: Color(0xFFC2524D),
            textSelectionColor: Colors.white,
            textTheme: TextTheme(
              subhead: TextStyle(color: Colors.white),
            ),
            buttonTheme: ButtonThemeData(
              buttonColor: Color(0xFFC2524D),
              highlightColor: Color(0xFF9B4040),
              splashColor: Color(0xFF9B4040),
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(0))),
            )),
        home: store.state.userInfo != null ? HomePage() : LoginFirst(),
        routes: {'/home': (BuildContext context) => HomePage()},
      ),
    );
  }
}

Future<void> main() async {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
    statusBarColor: Colors.transparent,
  ));
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  final JPush jpush = JPush();
  jpush.setup(
    appKey: '868cc9036e14c71e2b698f46',
    channel: 'theChannel',
    production: true,
    debug: true,
  );
  jpush.applyPushAuthority(NotificationSettingsIOS(sound: true, alert: true, badge: true));
  final prefs = await SharedPreferences.getInstance();
  print(prefs.getString('token'));
  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) {});
  }
  Store<AppState> store = Store<AppState>(appReducer, initialState: state);
  runApp(MobileCyberGamesApp(store));
}
