import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';

part 'agenda.g.dart';

@JsonSerializable()
class Agenda {
  @JsonKey(name: 'id', fromJson: _intToString, toJson: _stringToInt)
  String id;

  @JsonKey(name: 'talkId')
  String talkId;

  @JsonKey(name: 'name')
  String? name;

  @JsonKey(name: 'content')
  final Rx<String> _content = ''.obs;

  @JsonKey(name: 'createTime')
  String? createTime;

  @JsonKey(name: 'updateTime')
  String? updateTime;

  @JsonKey(ignore: true)
  RxBool todo = false.obs;

  @JsonKey(name: 'example')
  bool? isExample;

  @JsonKey(ignore: true)
  Rxn<AgendaStatus>? agendaStatus;

  Agenda(
      {required this.id,
      required this.talkId,
      this.name,
      this.createTime,
      this.updateTime,
      this.isExample});

  String get content => _content.value;

  set content(String newContent) {
    _content.value = newContent;
  }

  factory Agenda.fromJson(Map<String, dynamic> json) {
    final agenda = _$AgendaFromJson(json);
    agenda.todo.value =
        json.containsKey('todo') ? json['todo'] as bool? ?? false : false;
    bool? finish = json['finish'] as bool?;
    if (finish == true) {
      agenda.agendaStatus = Rxn(AgendaStatus.done);
    } else if (finish == false) {
      agenda.agendaStatus = Rxn(AgendaStatus.todo);
    }
    agenda._content.value = json['content'] as String? ?? '';
    return agenda;
  }

  static String _intToString(int value) => value.toString();

  static int _stringToInt(String value) => int.parse(value);
}

enum AgendaStatus {
  todo,
  completing,
  done,
}
