import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';

import '../feature/devices/device_model.dart';
import '../product/base/bloc/base_bloc.dart';
import '../product/constant/app/app_constants.dart';
import '../product/services/api_services.dart';
import '../product/shared/shared_snack_bar.dart';
import '../product/utils/date_time_utils.dart';

import '../product/utils/device_utils.dart';
import '../feature/device_log/device_logs_model.dart';

class DeviceLogsBloc extends BlocBase {
  APIServices apiServices = APIServices();
  final fromDate = StreamController<String>.broadcast();
  StreamSink<String> get sinkFromDate => fromDate.sink;
  Stream<String> get streamFromDate => fromDate.stream;

  final hasMore = StreamController<bool>.broadcast();
  StreamSink<bool> get sinkHasMore => hasMore.sink;
  Stream<bool> get streamHasMore => hasMore.stream;

  final allDevices = StreamController<List<Device>>.broadcast();
  StreamSink<List<Device>> get sinkAllDevices => allDevices.sink;
  Stream<List<Device>> get streamAllDevices => allDevices.stream;

  final sensors = StreamController<List<SensorLogs>>.broadcast();
  StreamSink<List<SensorLogs>> get sinkSensors => sensors.sink;
  Stream<List<SensorLogs>> get streamSensors => sensors.stream;

  final message = StreamController<String>.broadcast();
  StreamSink<String> get sinkmessage => message.sink;
  Stream<String> get streammessage => message.stream;

  @override
  void dispose() {}

  void getAllDevices(BuildContext context) async {
    try {
      List<Device> originalDevices = await apiServices.getOwnerDevices();
      List<Device> devices =
          DeviceUtils.instance.sortDeviceByState(originalDevices);
      sinkAllDevices.add(devices);
    } catch (e) {
      if (!context.mounted) return;
      showErrorTopSnackBarCustom(context, e.toString());
    }
  }

  void getDeviceLogByThingID(
    BuildContext context,
    int offset,
    String thingID,
    DateTime fromDate,
    List<SensorLogs> sensors,
  ) async {
    try {
      sinkmessage.add(ApplicationConstants.LOADING);
      String fromDateString =
          DateTimeUtils.instance.formatDateTimeToString(fromDate);
      String now =
          DateTimeUtils.instance.formatDateTimeToString(DateTime.now());
      Map<String, dynamic> params = {
        'thing_id': thingID,
        'from': fromDateString,
        'to': now,
        'limit': '30',
        "offset": offset.toString(),
        "asc": "true"
      };
      DeviceLog devicesListLog =
          await apiServices.getLogsOfDevice(thingID, params);
      if (devicesListLog.sensors!.isEmpty) {
        bool hasMore = false;
        sinkHasMore.add(hasMore);
      }
      if (devicesListLog.sensors!.isNotEmpty) {
        for (var sensor in devicesListLog.sensors!) {
          sensors.add(sensor);
        }
      } else {
        sinkmessage.add(ApplicationConstants.NO_DATA);
      }
      sinkSensors.add(sensors);
    } catch (e) {
      if (!context.mounted) return;
      showErrorTopSnackBarCustom(context, e.toString());
    }
  }
}
