import 'dart:developer';
import 'dart:io';

import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:terepi_seged/routes/app_pages.dart';

class StartPageController extends GetxController {
  late PermissionStatus bluetoothStatus;
  late Directory? applicationDocumentDirectory;
  late Directory? applicationSupportDirectory;
  late List<Directory>? externalCacheDirectories;
  late Directory? externalStorageDirectory;
  late Directory? temporaryDirectory;
  late List<Directory>? externalStorageDirectoriesDocuments;

  @override
  void onInit() async {
    applicationDocumentDirectory = await getApplicationDocumentsDirectory();
    applicationSupportDirectory = await getApplicationSupportDirectory();
    externalCacheDirectories = await getExternalCacheDirectories();
    externalStorageDirectory = await getExternalStorageDirectory();
    temporaryDirectory = await getTemporaryDirectory();
    externalStorageDirectoriesDocuments =
        await getExternalStorageDirectories(type: StorageDirectory.documents);

    if (applicationDocumentDirectory != null) {
      log("ApplicationDocumentsDirectory: ${applicationDocumentDirectory?.path}");
      String pointsPath = '${applicationDocumentDirectory!.path}/Points/';
      bool pointsPathIsExits = await Directory(pointsPath).exists();
      if (!pointsPathIsExits) {
        await Directory(pointsPath).create(recursive: true);
      }
      File dataFile = File("$pointsPath/data.txt");
      DateTime now = DateTime.now();
      String contents =
          "Start: ${DateFormat('kk:mm:ss \n EEE d MMM').format(now)} \r\n";
      await dataFile.writeAsString(contents, mode: FileMode.append);
      log(contents);
    } else {
      log("ApplicationDocumentsDirectory:  ---- NO ----}");
    }
    if (applicationSupportDirectory != null) {
      log("applicationSupportDirectory: ${applicationSupportDirectory?.path}");
    } else {
      log("applicationSupportDirectory:  ---- NO ----}");
    }
    if (externalCacheDirectories!.isNotEmpty) {
      log("externalCacheDirectories: ${externalCacheDirectories!.length}");
      for (var externalCacheDirectory in externalCacheDirectories!) {
        log("   - externalCacheDirectories: ${externalCacheDirectory.path}");
      }
    } else {
      log("externalCacheDirectories:  ---- NO ----}");
    }
    if (externalStorageDirectory != null) {
      print("externalStorageDirectory: ${externalStorageDirectory?.path}");
      String pointsPath = '${externalStorageDirectory!.path}/Points/';
      bool pointsPathIsExits = await Directory(pointsPath).exists();
      if (!pointsPathIsExits) {
        await Directory(pointsPath).create(recursive: true);
      }
      File dataFile = File("$pointsPath/data.txt");
      DateTime now = DateTime.now();
      String contents =
          "Start: ${DateFormat('kk:mm:ss \n EEE d MMM').format(now)} \r\n";
      await dataFile.writeAsString(contents, mode: FileMode.append);
      print(contents);
    } else {
      log("externalStorageDirectory:  ---- NO ----}");
    }
    if (temporaryDirectory != null) {
      log("temporaryDirectory: ${temporaryDirectory?.path}");
    } else {
      log("temporaryDirectory:  ---- NO ----}");
    }
    if (externalStorageDirectoriesDocuments!.isNotEmpty) {
      log("externalStorageDirectoriesDocuments: ${externalStorageDirectoriesDocuments!.length}");
      for (var externalStorageDirectoryDocuments
          in externalStorageDirectoriesDocuments!) {
        log("   - externalCacheDirectories: ${externalStorageDirectoryDocuments.path}");
      }
    } else {
      log("externalStorageDirectoriesDocuments:  ---- NO ----}");
    }

    super.onInit();
  }

  @override
  void onReady() async {
    super.onReady();
    bluetoothStatus = await Permission.bluetooth.status;
    _departStartPage();
  }

  void _departStartPage() async {
    await Future.delayed(const Duration(seconds: 5));
    //Get.offAllNamed(Routes.HOME);
    Get.offAllNamed(Routes.SHELL);
  }
}
