import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class ScheduleEntity {
  final bool enabled;
  final bool continuousDay;
  final TimeOfDay? range1Hour1;
  final TimeOfDay? range1Hour2;
  final TimeOfDay? range2Hour1;
  final TimeOfDay? range2Hour2;

  const ScheduleEntity({
    required this.enabled,
    required this.continuousDay,
    required this.range1Hour1,
    required this.range1Hour2,
    required this.range2Hour1,
    required this.range2Hour2,
  });

  static const empty = ScheduleEntity(
    enabled: false,
    continuousDay: false,
    range1Hour1: null,
    range1Hour2: null,
    range2Hour1: null,
    range2Hour2: null,
  );

  ScheduleEntity copyWith({
    bool? enabled,
    bool? continuousDay,
    TimeOfDay? range1Hour1,
    TimeOfDay? range1Hour2,
    TimeOfDay? range2Hour1,
    TimeOfDay? range2Hour2,
  }) {
    return ScheduleEntity(
      enabled: enabled ?? this.enabled,
      continuousDay: continuousDay ?? this.continuousDay,
      range1Hour1: range1Hour1 ?? this.range1Hour1,
      range1Hour2: range1Hour2 ?? this.range1Hour2,
      range2Hour1: range2Hour1 ?? this.range2Hour1,
      range2Hour2: range2Hour2 ?? this.range2Hour2,
    );
  }

  static ScheduleEntity fromDocument(Map<String, dynamic> doc) {
    return ScheduleEntity(
      enabled: doc['habilitado'] as bool,
      continuousDay: doc['continuous_day'] as bool,
      range1Hour1: parseTime(doc['range1Hour1']),
      range1Hour2: parseTime(doc['range1Hour2']),
      range2Hour1: parseTime(doc['range2Hour1']),
      range2Hour2: parseTime(doc['range2Hour2']),
    );
  }

  static TimeOfDay? parseTime(String? time) {
    try {
      if (time == null) return null;
      // Handle ISO8601 from backend (e.g. "1970-01-01T08:30:00.000Z")
      final t = time.contains('T') ? time.split('T')[1] : time;
      final components = t.split(':');
      if (components.length < 2) return null;
      return TimeOfDay(hour: int.parse(components[0]), minute: int.parse(components[1]));
    } catch (e) {
      return null;
    }
  }

  Map<String, dynamic> toJson() {
    return {
      'habilitado': enabled,
      'continuous_day': continuousDay,
      'range1Hour1': formatTimeOfDay(range1Hour1),
      'range1Hour2': formatTimeOfDay(range1Hour2),
      'range2Hour1': formatTimeOfDay(range2Hour1),
      'range2Hour2': formatTimeOfDay(range2Hour2),
    };
  }

  String? formatTimeOfDay(TimeOfDay? time) {
    if (time != null) {
      return "${time.hour.toString()}:${time.minute.toString()}";
    }
    return null;
  }

  static String? getFormatTime(TimeOfDay? time) {
    if (time == null) {
      return null;
    }
    final now = DateTime.now();
    final dateTime =
        DateTime(now.year, now.month, now.day, time.hour, time.minute);
    final format = DateFormat.jm();
    return format.format(dateTime);
  }

  @override
  String toString() {
    return '''ScheduleEntity {
      enabled: $enabled,
      continuousDay: $continuousDay,
      range1Hour1: $range1Hour1,
      range1Hour2: $range1Hour2,
      range2Hour1: $range2Hour1,
      range2Hour2: $range2Hour2 
    }''';
  }
}
