import 'dart:ui';
import 'package:http/http.dart' as http;

import 'package:flutter/material.dart';
import 'package:memorias_calculo/src/back/scoped_models/user_info_model.dart';
import 'package:memorias_calculo/src/front/pages/calculadora_ambiental_page.dart';
import 'package:memorias_calculo/src/front/pages/conectando_page.dart';
import 'package:memorias_calculo/src/front/pages/error_page.dart';
import 'package:memorias_calculo/src/front/pages/formulario_usuario_page.dart';
import 'package:memorias_calculo/src/front/pages/index_page.dart';
import 'package:memorias_calculo/src/front/pages/ventilacion_ambiental_page.dart';
import 'package:memorias_calculo/src/front/pages/ventilacion_localizada_page.dart';
import 'package:memorias_calculo/src/front/pages/ventilacion_estacionamientos_page.dart';
import 'package:scoped_model/scoped_model.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
  bool _errorConexion = false;

  @override
  void initState() {
    super.initState();

    _connectServer();
  }

  void _connectServer() async{
    final url = Uri.parse("https://smart.solerpalau.mx/QA/api/v1/loginCliente");

    try{
      await http.post(url);
    }catch(e){
      _errorConexion = true;
    }

    if(_errorConexion) {
      _navigatorKey.currentState!.pushReplacementNamed("error");
    }else{
      _navigatorKey.currentState!.pushReplacementNamed("formulario");
    }
  }

  @override
  Widget build(BuildContext context) {
    final UserInfoModel _userInfoModel = UserInfoModel();

    return ScopedModel<UserInfoModel>(
      model: _userInfoModel,
      child: MaterialApp(
        navigatorKey: _navigatorKey,
        title: "Portal de memorias de cálculo",
        debugShowCheckedModeBanner: false,
        scrollBehavior: ScrollRules(),
        theme: ThemeData(
          brightness: Brightness.light,
          primaryColor: const Color(0xFFFF0000),
          splashColor: const Color.fromRGBO(180, 4, 4, 1),
          colorScheme: const ColorScheme.light(
            primary: Color.fromRGBO(180, 4, 4, 1),
            secondary: Color.fromRGBO(180, 4, 4, 1)
          ),
        ),
        initialRoute: "conectando",
        onGenerateRoute: (RouteSettings settings){
          var routes = <String, WidgetBuilder>{
            "conectando": (ctx) => ConectandoPage(),
            "error"     : (ctx) => ErrorPage(),
            "formulario": (ctx) => FormularioUsuarioPage(),
            "index"     : (ctx) => IndexPage(errorConexion: _errorConexion),
            "ventilacionLocalizada" : (ctx) => VentilacionLocalizadaPage(info: settings.arguments!),
            "ventilacionEstacionamientos" : (ctx) => VentilacionEstacionamientosPage(info: settings.arguments!),
            "ventilacionAmbiental" : (ctx) => VentilacionAmbientalPage(info: settings.arguments!, errorConexion: _errorConexion),
          };
          WidgetBuilder builder = routes[settings.name]!;
          return MaterialPageRoute(builder: (ctx) => builder(ctx));
        },
      ),
    );
  }
}

class ScrollRules extends MaterialScrollBehavior{
  // Override behavior methods and getters like dragDevices
  @override
  Set<PointerDeviceKind> get dragDevices => {
    PointerDeviceKind.touch,
    PointerDeviceKind.mouse,
    // etc.
  };
}
