import 'package:auto_size_text/auto_size_text.dart';
import 'package:design/design.dart';
import 'package:get/get.dart';
import 'package:guru_widgets/guru_widgets.dart';
import 'package:guru_widgets/button/guru_button.dart';
import 'package:guru_utils/router/router.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

import 'editor_controller.dart';
import 'editor_design_model.dart';

/// Created by Haoyi on 2022/8/19
///
class EditorDialog extends GetWidget<EditorController> {
  @override
  String? get tag => "EditorDialog";

  final String title;
  final Widget? tips;
  final String? hint;
  final String positiveText;
  final String? negativeText;
  final void Function(String text)? onPositive;
  final void Function(String text)? onNegative;

  EditorDialogDesignSpec get dialogDesignModel => controller.dialogDesignModel;

  EditorDialog(
      {Key? key,
      this.title = "Editor",
      this.hint = "editor",
      this.tips,
      this.positiveText = "ok",
      this.negativeText,
      this.onPositive,
      this.onNegative})
      : super(key: key) {
      Get.create<EditorController>(() => EditorController(),
        tag: tag, permanent: false);
  }

  @override
  Widget build(BuildContext context) {
    final hasNegative = negativeText != null && onNegative != null;
    return Center(
      child: FlexibleContainer(
          constraints: BoxConstraints(
              minWidth: dialogDesignModel.measuredSize.width,
              maxWidth: dialogDesignModel.measuredSize.width,
              minHeight: dialogDesignModel.measuredSize.height),
          radius: dialogDesignModel.radius,
          color: const Color(0xFF252525),
          padding: EdgeInsets.only(bottom: dialogDesignModel.bottomMargin),
          child: DecoratedBox(
              decoration: BoxDecoration(
                borderRadius: dialogDesignModel.radius,
                color: const Color(0xFF252525),
                gradient: const LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [Color(0xFF242424), Color(0xFF1D1D1D)]),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  SizedSpacer(height: dialogDesignModel.headerTopSpacing),
                  Align(
                    alignment: AlignmentDirectional.topStart,
                    child: Padding(
                        padding: dialogDesignModel.headerPadding,
                        child: SizedBox(
                          height: dialogDesignModel.headerHeight,
                          child: Stack(
                            children: [
                              Align(
                                alignment: AlignmentDirectional.topStart,
                                child: TapWidget(
                                  onTap: () async {
                                    RouteCenter.instance.back();
                                  },
                                  child: Image.asset(
                                    "assets/images/ic_close.png",
                                    width: dialogDesignModel.closeIconSize,
                                    height: dialogDesignModel.closeIconSize,
                                  ),
                                ),
                              ),
                              Align(
                                alignment: Alignment.bottomCenter,
                                child: AutoSizeText(
                                  title,
                                  minFontSize: 10,
                                  maxLines: 1,
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                      fontSize: dialogDesignModel.titleFontSize,
                                      fontWeight: GuruTheme.fwBold,
                                      color: Colors.white),
                                ),
                              )
                            ],
                          ),
                        )),
                  ),
                  SizedSpacer(height: dialogDesignModel.textFieldTopSpacing),
                  FlexibleContainer(
                    height: dialogDesignModel.textFieldHeight,
                    width: dialogDesignModel.buttonSize.width,
                    radius: dialogDesignModel.textFieldRadius,
                    color: Colors.white.withOpacity(0.07),
                    padding: dialogDesignModel.textFieldHorizontalPadding,
                    needClip: true,
                    alignment: AlignmentDirectional.centerStart,
                    child: TextField(
                        controller: controller.editorController,
                        focusNode: controller.focusNode,
                        showCursor: true,
                        // autofocus: true,
                        // maxLengthEnforcement: MaxLengthEnforcement.enforced,
                        // buildCounter: _buildCounter,
                        style: TextStyle(
                            fontSize: dialogDesignModel.textFieldFontSize,
                            fontWeight: GuruTheme.fwMedium,
                            color: Colors.white),
                        maxLines: 1,
                        onChanged: (_) {},
                        decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: hint,
                            hintStyle: TextStyle(
                                fontSize: dialogDesignModel.textFieldFontSize,
                                fontWeight: GuruTheme.fwMedium,
                                color: const Color(0xFF808080))
                            // filled: true,
                            )),
                  ),
                  if (tips != null) ...[
                    SizedSpacer(height: dialogDesignModel.tipsTopSpacing),
                    tips!
                  ],
                  SizedSpacer(height: dialogDesignModel.buttonVerticalSpacing),
                  Row(
                    children: [
                      if (hasNegative) ...[
                        Expanded(
                          flex: 1,
                          child: Center(
                            child: GuruButton(
                                size: dialogDesignModel.smallButtonSize,
                                style: GuruButtonStyle.negative,
                                action: negativeText!,
                                onPressed: () async {
                                  onNegative?.call(controller.text);
                                }),
                          ),
                        ),
                      ],
                      Expanded(
                        flex: 1,
                        child: Center(
                          child: GuruButton(
                              size: hasNegative
                                  ? dialogDesignModel.smallButtonSize
                                  : dialogDesignModel.buttonSize,
                              action: positiveText,
                              onPressed: () async {
                                if (onPositive != null) {
                                  onPositive?.call(controller.text);
                                } else {
                                  RouteCenter.instance.back();
                                }
                              }),
                        ),
                      ),
                    ],
                  ),
                  SizedSpacer(height: dialogDesignModel.buttonVerticalSpacing),
                ],
              ))),
    );
  }
}