import 'dart:async';

import 'package:aitrainer_app/util/app_language.dart';
import 'package:aitrainer_app/util/app_localization.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
//import 'package:health/health.dart';
import 'package:meta/meta.dart';

part 'settings_event.dart';
part 'settings_state.dart';

class SettingsBloc extends Bloc<SettingsEvent, SettingsState> with Logging {
  String? language;
  Locale? _locale;
  BuildContext? context;

  SettingsBloc({this.context}) : super(SettingsInitial());

  void setLocale(Locale locale) {
    _locale = locale;
  }

  Locale? getLocale() {
    return _locale;
  }

  @override
  Stream<SettingsState> mapEventToState(
    SettingsEvent event,
  ) async* {
    if (event is SettingsChangeLanguage) {
      yield SettingsLoading();
      await _changeLang(event.language);
      yield SettingsReady();
    } else if (event is SettingsGetLanguage) {
      await AppLanguage().fetchLocale();
      _locale = AppLanguage().appLocal;
      yield SettingsReady();
    } else if (event is SettingsSetServer) {
      //yield SettingsLoading();
      final bool live = event.live;
      Cache().setServer(live);
      Track().track(TrackingEvent.settings_server, eventValue: live.toString());
      //yield SettingsReady(_locale);
    } else if (event is SettingsSetHardware) {
      yield SettingsLoading();

      bool selectedHardwareBefore = await Cache().selectedHardwareBefore();
      log("selectedBefore " + selectedHardwareBefore.toString());

      final bool hasHardware = event.hasHardware;
      await Cache().setHardware(hasHardware);
      if (hasHardware == true) {
        await _accessHealthData();
      }
      Cache().initBadges();
      yield SettingsReady();
    } else if (event is SettingsActivateTutorial) {
      yield SettingsLoading();
      Cache().activitiesDone[event.activity.toStr()] = false;
      print(" ----------------- Setting ${event.activity} to false");
      yield SettingsReady();
    }
  }

  Future<void> _accessHealthData() async {
    /* final List<HealthDataType> types = [
      HealthDataType.ACTIVE_ENERGY_BURNED,
      HealthDataType.WATER,
      HealthDataType.STEPS,
      HealthDataType.HEART_RATE,
      HealthDataType.BASAL_ENERGY_BURNED,
      HealthDataType.BODY_TEMPERATURE,
      HealthDataType.HIGH_HEART_RATE_EVENT,
      HealthDataType.LOW_HEART_RATE_EVENT,
      HealthDataType.RESTING_HEART_RATE
    ];
    final HealthFactory health = HealthFactory(); */
    //DateTime now = DateTime.now();
    //List<HealthDataPoint> _healthDataList = await health.getHealthDataFromTypes(now.subtract(Duration(minutes: 5)), now, types);
    //log(_healthDataList.toString());
  }

  Future<void> _changeLang(String lang) async {
    log("_change to $lang");
    switch (lang) {
      case "English":
      case "en":
      case "Angol":
        _locale = Locale('en');
        break;
      case "Hungarian":
      case "Magyar":
      case "hu":
        _locale = Locale('hu');
        break;
    }
    this.language = lang;
    AppLanguage().changeLanguage(_locale!);
    await loadLang();
    Cache().initBadges();
  }

  Future<void> loadLang() async {
    if (_locale != null) {
      log(" -- Loading lang $_locale");
      if (context != null) {
        AppLocalizations.of(context!)!.setLocale(_locale!);
        await AppLocalizations.of(context!)!.load();
      } else {
        log("no context, does not load");
      }
    } else {
      log("no locale, does not load");
    }
  }
}
