import 'package:vital_local_database/core/interface/entity.dart';
import 'package:vital_local_database/core/sqlite/entity.dart';

import 'defines.dart';

/// 可以同步实体
abstract class SyncableEntity<T extends BaseEntity> extends BaseEntity<T> {
  /// 唯一编码
  String code = '';

  /// 数据Json
  String dataJson = '';

  /// 同步类型
  OfflineDataSyncType syncType = OfflineDataSyncType.create;

  /// 同步状态
  OfflineDataSyncState syncState = OfflineDataSyncState.wait;

  @override
  Map<String, dynamic> toJson() {
    final map = super.toJson();
    map['code'] = code;
    map['dataJson'] = dataJson;
    map['syncType'] = syncType.index;
    map['syncState'] = syncState.index;
    return map;
  }

  @override
  T fromJson(Map<String, dynamic> map) {
    final T entity = super.fromJson(map);
    code = map['code'];
    dataJson = map['dataJson'];
    syncType = OfflineDataSyncType.values[map["syncType"]];
    syncState = OfflineDataSyncState.values[map["syncState"]];
    return entity;
  }
}

abstract class SyncableColumnsDefine<T extends BaseEntity>
    extends BaseColumnsDefine<T> implements IDbColumnsDefine<T> {
  final code = DbColumn<String>("code");
  final dataJson = DbColumn<String>("dataJson");
  final syncType = DbColumn<OfflineDataSyncType>("syncType");
  final syncState = DbColumn<OfflineDataSyncState>("syncState");
}
