/*
 * @Author: XianKaiQun
 * @Date: 2020-06-22 21:47:54
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-05-25 15:36:05
 * @Description: 
 */
import 'package:flutter/material.dart';

import 'package:wisdom_cli/wisdom_cli.dart';

class WisCellTheme extends InheritedTheme {
  const WisCellTheme({
    Key? key,
    this.color,
    this.titleTextStyle,
    this.subTitleTextStyle,
    this.iconThemeData,
    required Widget child,
  }) : super(key: key, child: child);

  final Color? color;

  final TextStyle? titleTextStyle;

  final TextStyle? subTitleTextStyle;

  final IconThemeData? iconThemeData;

  static final TextStyle defaultTitleTextStyle = TextStyle(
    color: Colors.black,
    fontSize: 15,
  );

  static final TextStyle defaultSubTitleTextStyle = TextStyle(
    color: Colors.black45,
    fontSize: 12,
  );

  static final IconThemeData defaultIconThemeData = IconThemeData(
    color: Colors.black54,
    size: 20,
  );

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    throw UnimplementedError();
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    throw UnimplementedError();
  }

  static WisCellTheme of(BuildContext context) {
    final WisCellTheme? result =
        context.dependOnInheritedWidgetOfExactType<WisCellTheme>();
    return result ?? WisCellTheme(child: Text(''));
  }

  Color? getColor(WisCell cell) {
    return cell.color ?? color;
  }

  TextStyle getTitleTextStyle(WisCell cell) {
    return defaultTitleTextStyle
        .merge(titleTextStyle)
        .merge(cell.titleTextStyle);
  }

  TextStyle getSubTitleTextStyle(WisCell cell) {
    return defaultSubTitleTextStyle
        .merge(subTitleTextStyle)
        .merge(cell.subTitleTextStyle);
  }

  IconThemeData getIconThemeData(WisCell cell) {
    return defaultIconThemeData.merge(iconThemeData).merge(cell.iconThemeData);
  }
}

enum WisCellDivider {
  none,
  normal,
  fill,
}

class WisCell extends StatelessWidget {
  const WisCell({
    Key? key,
    this.leading,
    required this.title,
    this.subtitle,
    this.trailing,
    this.topDivider,
    this.bottomDivider,
    this.padding,
    this.enabled = true,
    this.onTap,
    this.onLongPress,
    this.selected = false,
    this.height,
    this.color,
    this.titleTextStyle,
    this.subTitleTextStyle,
    this.iconThemeData,
    this.interval,
  }) : super(key: key);

  final Widget? leading;

  final Widget title;

  final Widget? subtitle;

  final Widget? trailing;

  final double? height;
  final double? interval;
  final WisCellDivider? topDivider;

  final WisCellDivider? bottomDivider;

  final Color? color;

  final TextStyle? titleTextStyle;

  final TextStyle? subTitleTextStyle;

  final IconThemeData? iconThemeData;

  final EdgeInsetsGeometry? padding;

  final bool enabled;

  final GestureTapCallback? onTap;

  final GestureLongPressCallback? onLongPress;

  final bool selected;
  BorderSide get _borderSide => BorderSide(width: 0.3, color: Colors.black26);
  bool get _hasTopDivider => topDivider == WisCellDivider.fill;
  bool get _hasBottomDivider => bottomDivider == WisCellDivider.fill;
  bool get _hasNormalTopDivider => topDivider == WisCellDivider.normal;
  bool get _hasNormalBottomDivider => bottomDivider == WisCellDivider.normal;
  @override
  Widget build(BuildContext context) {
    WisCellTheme wisCellTheme = WisCellTheme.of(context);

    return IconTheme(
      data: wisCellTheme.getIconThemeData(this),
      child: Wisdom.row(
        // padding: contentPadding,
        color: wisCellTheme.getColor(this),
        padding: padding ?? EdgeInsets.all(10.pt),
        height: height,
        border: Border(
          top: _hasTopDivider ? _borderSide : BorderSide.none,
          bottom: _hasBottomDivider ? _borderSide : BorderSide.none,
        ),
        inkWell: WisBoxInkWell(
          onTap: onTap != null ? onTap : null,
          onLongPress: onLongPress != null ? onLongPress : null,
        ),
        children: <Widget>[
          if (leading != null) SizedBox(width: interval ?? 10.pt),
          if (leading != null) leading!,
          if (leading != null) SizedBox(width: interval ?? 10.pt),
          Expanded(
            child: Wisdom.row(
              border: Border(
                top: _hasNormalTopDivider ? _borderSide : BorderSide.none,
                bottom: _hasNormalBottomDivider ? _borderSide : BorderSide.none,
              ),
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    DefaultTextStyle(
                      style: wisCellTheme.getTitleTextStyle(this),
                      child: title,
                    ),
                    if (subtitle != null) SizedBox(height: 5.pt),
                    if (subtitle != null)
                      DefaultTextStyle(
                        style: wisCellTheme.getSubTitleTextStyle(this),
                        child: subtitle!,
                      ),
                  ],
                ).asExpanded(),
                if (trailing != null) SizedBox(width: interval ?? 10.pt),
                if (trailing != null) trailing!,
              ],
            ),
          ),
        ],
      ),
    );
  }
}
