import 'dart:io';

import 'package:modelo/atualizarapp/atualizarapp.dart';
import 'package:modelo/index/index.dart';
import 'package:modelo/notificacoes/notificacoes.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../funcoes_gerais.dart';

class SplashController extends GetxController {
  //bool _tutorialOk = false;
  //FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  //FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
  }

  // Future<void> verTutorial()async{
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   this._tutorialOk = prefs.getBool('tutorial') ?? false;
  // }

  Future<Widget> verUsuLogado() async {
    var resp = await FuncoesGerais.checkVersaoMinimaOk();
    if (resp.vOk!) {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      var token = prefs.getString('token_notif');
      await FuncoesGerais.registraDevice(token);
      return IndexScreen();
    } else {
      return AtualizarScreen(linkLoja: resp.linkLoja);
    }
  }

  Future<void> firebaseCloudMessagingListeners() async {
    try {
      //if (_firebaseMessaging == null) return;

      if (Platform.isIOS) iOSPermission();

      // _firebaseMessaging.getToken().then((token) async {
      //   print(token);
      //   SharedPreferences prefs = await SharedPreferences.getInstance();
      //   await prefs.setString('token_notif', token);
      // });
      FirebaseMessaging.instance.getToken().then((token) async {
        print(token);
        SharedPreferences prefs = await SharedPreferences.getInstance();
        await prefs.setString('token_notif', token!);
      });
      // _firebaseMessaging.configure(
      //   onLaunch: this._onNotification,
      //   onMessage: this._onNotification,
      //   //onBackgroundMessage: this._onNotification,
      //   onResume: this._onNotification
      // );
      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        print('Push onMessage');
        Get.snackbar(message.notification!.title ?? 'Notificação',
            message.notification!.body ?? 'Nova notificação',
            mainButton: TextButton(
                onPressed: () => this._onNotification(message),
                child: Text('Ver')),
            backgroundColor: Colors.black.withOpacity(0.7),
            colorText: Colors.white);
        //this._onNotification(message);
      });
      FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        print('A new onMessageOpenedApp event was published!');
        print(message);
        //Get.snackbar(message.notification!.title ?? 'Notificação', message.notification!.body ?? 'Nova notificação',mainButton: TextButton(onPressed: () => this._onNotification(message), child: Text('Ver')));
      });
    } on PlatformException catch (e) {
      print(e.message);
    }
  }

  void _onNotification(RemoteMessage message) {
    Get.to(() => NotificacoesScreen());
    //print(message);
    //print(message['aps']['alert']['title']);
    //Get.rawSnackbar(message: message['aps']['alert']['title']);
  }

  void iOSPermission() {
    FirebaseMessaging.instance.requestPermission();
    // _firebaseMessaging.requestNotificationPermissions(
    //     IosNotificationSettings(sound: true, badge: true, alert: true));
    // _firebaseMessaging.onIosSettingsRegistered
    //     .listen((IosNotificationSettings settings) {
    //   print("Settings registered: $settings");
    // });
  }
}
