import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';

import '../Notifiers/splashVersionNotifier.dart';

class Splash extends StatefulWidget {
  const Splash({super.key});

  @override
  State<Splash> createState() => _SplashState();
}

class _SplashState extends State<Splash> {
  late Image splashImage;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    splashImage = Image.asset("assets/images/ic_splash.jpg");
    WidgetsBinding.instance.addPostFrameCallback((_) {
      requestPermissions();
      final spalshProvider = Provider.of<SplashVersionNotifier>(
        context,
        listen: false,
      );
      spalshProvider.initPackageInfo();
      spalshProvider.handleVersionCheck(context);
    },);


  }

  void requestPermissions() async {
    await getCameraPermissions();
    await getStoragePermission();
    await getLocationPermissions();
  }

  Future<void> getStoragePermission() async {
    if (await Permission.manageExternalStorage.request().isGranted) {
      print("Storage");
      setState(() {});
    } else if (await Permission.storage.request().isPermanentlyDenied) {
      print("Storage");
      await openAppSettings();
    } else if (await Permission.storage.request().isDenied) {
      print("Storage");
      setState(() {});
    }
  }

  Future<void> getCameraPermissions() async {
    if (await Permission.camera.request().isGranted) {
      print("Camera1");
      setState(() {});
    } else if (await Permission.camera.request().isPermanentlyDenied) {
      print("Camera2");
      if (Platform.isAndroid) {
        await openAppSettings();
      } else if (Platform.isIOS) {
        setState(() {});
      }
    } else if (await Permission.camera.request().isDenied) {
      print("Camera3");
      setState(() {});
    }
  }

  Future<void> getLocationPermissions() async {
    if (await Permission.location.request().isGranted) {
      print("Location1a");
      setState(() {});
    } else if (await Permission.location.request().isPermanentlyDenied) {
      print("Location1b");
      if (Platform.isAndroid) {
        await openAppSettings();
      } else if (Platform.isIOS) {
        // await AppSettings.openAppSettings(type: AppSettingsType.location);
      }
    } else if (await Permission.location.request().isDenied) {
      print("Location1c");
      setState(() {});
    }

    if (await Permission.locationAlways.request().isGranted) {
      print("Location2a");
      setState(() {});
    } else if (await Permission.locationAlways.request().isPermanentlyDenied) {
      print("Location2b");
      if (Platform.isAndroid) {
        await openAppSettings();
      } else if (Platform.isIOS) {
        // await AppSettings.openAppSettings(type: AppSettingsType.location);
      }
    } else if (await Permission.locationAlways.request().isDenied) {
      print("Location2c");
      setState(() {});
    }

    if (await Permission.locationWhenInUse.request().isGranted) {
      print("Location3a");
      setState(() {});
    } else if (await Permission.locationWhenInUse
        .request()
        .isPermanentlyDenied) {
      print("Location3b");
      if (Platform.isAndroid) {
        await openAppSettings();
      } else if (Platform.isIOS) {
        // await AppSettings.openAppSettings(type: AppSettingsType.location);
      }
    } else if (await Permission.locationWhenInUse.request().isDenied) {
      print("Location3c");
      setState(() {});
    }
  }

  Future<void> getNotificationsPermissions() async {
    if (await Permission.notification.request().isGranted) {
      setState(() {});
    } else if (await Permission.notification.request().isPermanentlyDenied) {
      await openAppSettings();
    } else if (await Permission.notification.request().isDenied) {
      setState(() {});
    }
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    precacheImage(splashImage.image, context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: SafeArea(
        child: Consumer<SplashVersionNotifier>(
          builder: (context, value, child) {
            return Container(
              alignment: Alignment.center,
              child: Container(
                alignment: Alignment.center,
                height: 300,
                width: 300,
                child: splashImage,
              ),
            );
          },
        ),
      ),
    );
  }
}
