import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:ont_app2/model/ont_data_model.dart';
import 'package:ont_app2/model/ont_model.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';

import '../../../app/app.locator.dart';
import '../../../app/app.logger.dart';
import '../../../app/app.router.dart';
import '../../../services/http_services.dart';
import '../../../services/my_notification.dart';
import '../../../services/my_preferences.dart';
import '../../../services/my_socket_io_client.dart';
import '../../../services/other_function.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

class NavBarViewModel extends IndexTrackingViewModel {
  final log = getLogger('NavBarViewModel');
  final _navigationService = locator<NavigationService>();
  final _mySharedPrefs = locator<MySharedPrefs>();
  final _socketIoClient = locator<MySocketIoClient>();
  final _myNotification = locator<MyNotification>();
  final _otherFunction = locator<OtherFunction>();
  final _httpServices = locator<MyHttpServices>();

  final _bottomNavBarList = [
    {
      'name': 'Monitoring',
      'icon': Icons.home_outlined,
    },
    {
      'name': 'Log Data',
      'icon': Icons.list_alt_outlined,
    },
  ];

  List<Map<String, dynamic>> get bottomNavBarList => _bottomNavBarList;

  final List<String> _views = [
    NavBarViewRoutes.monitoringView,
    NavBarViewRoutes.logDataView,
  ];

  Future<void> init() async {
    String? id = await _mySharedPrefs.getString('id');
    refreshData(id!);
    Timer.periodic(const Duration(seconds: 10), (timer) {
      log.i('timer dan refresh data');
      refreshData(id);
      refreshOnt(id);
    });
    // _socketIoClient.on('data', (data) {
    //   // log.i('data : $data');
    //   var waterHeight = data['water_height'];
    //   _socketIoClient.waterHeight = waterHeight is int
    //       ? waterHeight.toDouble()
    //       : waterHeight is double
    //           ? waterHeight
    //           : double.parse(waterHeight as String);

    //   _socketIoClient.warningLevel = data['warning_level'];
    //   _socketIoClient.dangerLevel = data['danger_level'];

    //   if (_socketIoClient.dangerLevel == 1) {
    //     _socketIoClient.status =
    //         "Bahaya , Peringatan Banjir, Air Melewati Batas";
    //     if (_socketIoClient.notif < 2) {
    //       _myNotification.showNotification(
    //         id: 1,
    //         title: 'Peringatan Banjir',
    //         body: 'Air Melewati Batas',
    //         payload: 'payload',
    //         flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin,
    //       );
    //       _socketIoClient.notif = 2;
    //     }
    //   } else if (_socketIoClient.warningLevel == 1) {
    //     _socketIoClient.status =
    //         "Peringatan Banjir, Air Dalam Skala 4:5 atau lebih";
    //     if (_socketIoClient.notif == 0) {
    //       _myNotification.showNotification(
    //         id: 2,
    //         title: 'Peringatan Banjir',
    //         body: 'Air Dalam Skala 4:5 atau lebih',
    //         payload: 'payload',
    //         flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin,
    //       );
    //       _socketIoClient.notif = 1;
    //     }
    //   } else {
    //     _socketIoClient.status = "Normal";
    //     _socketIoClient.notif = 0;
    //   }

    //   notifyListeners();
    // });
  }

  refreshData(String id) async {
    log.wtf("lakukan pengambilan data");
    setBusy(true);
    try {
      var res = await _httpServices.get('/data_ont?id=$id', stat: false);
      List<Map<String, dynamic>> data =
          List<Map<String, dynamic>>.from(res.data);

      List<OntDataModel> dataModel =
          data.map((e) => OntDataModel.fromJson(e)).toList();

      _otherFunction.listDataOnt.clear();
      _otherFunction.listDataOnt = dataModel;
      for (var i = 0; i < dataModel.length; i++) {
        log.i(dataModel[i].toJson());
      }
      notifyListeners();
    } catch (e) {
      log.e(e);
    } finally {
      setBusy(false);
    }
  }

  refreshOnt(String id) async {
    log.wtf("lakukan pengambilan data ont");
    setBusy(true);
    try {
      var res = await _httpServices.get('/data-ont/$id', stat: false);
      log.wtf(res.data);
      OntModel data = OntModel.fromJson(res.data);
      _otherFunction.ontModel = data;
      notifyListeners();
    } catch (e) {
      log.e(e);
      log.e("Disini yang error");
    } finally {
      setBusy(false);
    }
  }

  void handleNavigation(int index) {
    log.d("handleNavigation: $index");
    log.d("currentIndex: $currentIndex");

    if (currentIndex == index) return;

    setIndex(index);
    // header = _bottomNavBarList[index]['header'] as String;
    _navigationService.navigateTo(
      _views[index],
      id: 3,
    );
  }
}
