import 'package:flutter/material.dart';
import 'package:flut/flut/runtime.dart';
import 'package:flut/flut/object.dart';
import 'package:flut/dart/ui.dart';

class FlutTextStyle extends FlutImmutableObject {
  final TextStyle textStyle;

  const FlutTextStyle(this.textStyle) : super('TextStyle');

  @override
  Map<String, dynamic> flutEncode() {
    final result = flutBaseProps();
    if (textStyle.fontSize != null) result['fontSize'] = textStyle.fontSize;
    if (textStyle.fontWeight != null) {
      result['fontWeight'] = FlutFontWeight(textStyle.fontWeight!).flutEncode();
    }
    if (textStyle.color != null) {
      result['color'] = FlutColor(textStyle.color!).flutEncode();
    }
    if (textStyle.fontFamily != null)
      result['fontFamily'] = textStyle.fontFamily;
    if (textStyle.height != null) result['height'] = textStyle.height;
    return result;
  }

  static TextStyle? flutDecode(FlutRuntime runtime, Map<String, dynamic> data) {
    return TextStyle(
      fontSize: runtime.unpackOptionalField<double>(data, 'fontSize'),
      fontWeight: runtime.unpackOptionalField<FontWeight>(data, 'fontWeight'),
      color: runtime.unpackOptionalField<Color>(data, 'color'),
      fontFamily: runtime.unpackOptionalField<String>(data, 'fontFamily'),
      height: runtime.unpackOptionalField<double>(data, 'height'),
    );
  }
}
