import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tour_travel_agr/helper/prefs.dart';
import 'package:tour_travel_agr/resource/routes.dart';
import 'package:url_strategy/url_strategy.dart';

late SharedPreferences prefs;
bool debug = true;
String titleApp = 'Tour & Travel Agency';
String baseUrl = "https://hibiscus-dev.ravku.com/";
String endPoint = "agency/api/";

String initialRoute = Routes.splashRoute;
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  setPathUrlStrategy();
  configLoading();
  SharedPreferences.getInstance().then(
    (value) {
      prefs = value;
      List<String> pathUri = Uri.base.pathSegments;
      if (pathUri.length == 2) {
        if (pathUri[0] == Routes.resetPasswordRoute.replaceFirst("/", "")) {
          initialRoute = "${Routes.resetPasswordRoute}/${pathUri[1]}";
        }
      }
      PackageInfo.fromPlatform().then((packageInfo) {
        setAppVersion(packageInfo.version.toString());
      });
      runApp(const MyApp());
    },
  );
}

//easy loading
void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = true
    ..dismissOnTap = false;
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: titleApp,
      debugShowCheckedModeBanner: false,
      onGenerateRoute: RouteGenerator.getRoute,
      initialRoute: initialRoute,
      builder: EasyLoading.init(),
    );
  }
}
