// import 'package:firebase_core/firebase_core.dart';
// import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get_storage/get_storage.dart';

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:jwt_decoder/jwt_decoder.dart';
import 'package:IQ/app/modules/selectISP/models/isp_model.dart';
import 'package:IQ/app/modules/selectISP/views/select_isp_view.dart';
import 'package:IQ/app.dart';
import 'package:logger/logger.dart';

final storage = GetStorage();
String? token = '';
String? deviceId = '';
bool isExpired = false;
String lat = "";
String long = "";
Future<Position?> _getRequestPermission() async {
  LocationPermission permission = await Geolocator.requestPermission();
  if (permission == LocationPermission.deniedForever) {
    Geolocator.openAppSettings();
    return null; // Return null or handle this case differently.
  } else if (permission == LocationPermission.denied) {
    return null; // Return null or handle this case differently.
  } else {
    // If permission is granted, get the current position and return.
    return await Geolocator.getCurrentPosition();
  }
}

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback = (
        X509Certificate cert,
        String host,
        int port,
      ) =>
          true;
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // await Firebase.initializeApp();
  HttpOverrides.global = MyHttpOverrides();
  await GetStorage.init();
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.light,
    ),
  );
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  token = storage.read('token');
  selectedISP = storage.read('selectedISP') != null
      ? ISPsData.fromJson(storage.read('selectedISP'))
      : null;
  if (token != null) {
    isExpired = JwtDecoder.isExpired(token!);
    if (isExpired == true) {
      storage.remove('token');
    }
  }
  // final messaging = FirebaseMessaging.instance;
  // await messaging.requestPermission(
  //   alert: true,
  //   badge: true,
  //   sound: true,
  // );
  // deviceId = await messaging.getToken();
  Logger().e("deviceId = $deviceId");
  final currentPosition = await _getRequestPermission();
  if (currentPosition != null) {
    lat = currentPosition.latitude.toString();
    long = currentPosition.longitude.toString();
  }
  runApp(MyApp());
}
