import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:keyboard/base/base_view.dart';
import 'package:keyboard/resource/assets.gen.dart';
import 'package:keyboard/resource/colors.gen.dart';
import 'package:keyboard/widget/gradient_btn.dart';

import '../../../../../resource/string.gen.dart';
import '../../../../../widget/delegate_lifecycle_widget.dart';
import 'intimacy_generate_character_edit_controller.dart';

/// 保存时回调
typedef OnSaveCallback = void Function(String customDirection);

/// 关闭弹窗时回调
typedef OnCloseCallback = void Function();

/// 生成亲密度人设（生成键盘）的编辑弹窗的内容
class IntimacyGenerateCharacterEditView extends BaseView<IntimacyGenerateCharacterEditController> {
  final OnSaveCallback onSaveCallback;
  final OnCloseCallback onCloseCallback;

  const IntimacyGenerateCharacterEditView({
    super.key,
    required this.onSaveCallback,
    required this.onCloseCallback,
  });

  @override
  backgroundColor() => Colors.transparent;

  @override
  Widget buildBody(BuildContext context) {
    return DelegateLifecycleWidget(
      onCreateCallback: () {
        // 进入页面，就获取输入框焦点
        controller.doRequestInputFocus();
      },
      child: Stack(
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 24.h, horizontal: 16.w),
            decoration: BoxDecoration(
              color: ColorName.white,
              borderRadius: BorderRadius.all(Radius.circular(16.r)),
            ),
            child: Column(
              // 包裹内容
              mainAxisSize: MainAxisSize.min,
              children: [
                // 标题
                _buildTitle(),
                SizedBox(height: 16.h),
                // 输入框
                _buildInput(),
                SizedBox(height: 24.h),
                _buildSaveBtn(),
              ],
            ),
          ),
          Positioned(
            top: 0,
            right: 0,
            child: InkWell(
              onTap: () {
                controller.doClose(onCloseCallback);
              },
              splashColor: ColorName.transparent,
              child: Container(
                padding: EdgeInsets.all(14.w),
                child: Assets.images.iconCustomDirectionEditClose.image(
                  width: 24.w,
                  height: 24.w,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 标题
  Widget _buildTitle() {
    return Text(
      StringName.intimacyGenerateCharacterTitle,
      style: TextStyle(
        fontSize: 16.sp,
        color: ColorName.black80,
        fontWeight: FontWeight.bold,
      ),
    );
  }

  /// 输入框
  Widget _buildInput() {
    return TextField(
      // 点击输入框外部，关闭软键盘
      onTapUpOutside: (event) {
        controller.doInputUnFocus();
      },
      // 焦点控制
      focusNode: controller.inputFocusNode,
      // 输入框控制
      controller: controller.editingController,
      // 文字居中显示
      textAlign: TextAlign.center,
      // 单行
      maxLines: 1,
      // 设置光标颜色
      cursorColor: ColorName.inputCursor,
      // 光标宽度
      cursorWidth: 2.w,
      // 光标圆角
      cursorRadius: Radius.circular(2.r),
      // 输入框的边框样式
      decoration: InputDecoration(
        // 启用背景填充，背景才会生效
        filled: true,
        // 设置背景颜色
        fillColor: Color(0xFFF6F5FA),
        // 调整内边距
        contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
        // 设置边框样式
        border: OutlineInputBorder(
          // 圆角半径
          borderRadius: BorderRadius.circular(31.0),
          // 隐藏边框线
          borderSide: BorderSide.none,
        ),
        // 未输入时的提示文字
        hintText: StringName.intimacyGenerateCharacterInputHint,
        hintStyle: TextStyle(
          color: Colors.black45,
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
        ),
      ),
      style: TextStyle(
        color: ColorName.black80,
        fontSize: 14.sp,
        fontWeight: FontWeight.w400,
      ),
    );
  }

  /// 保存按钮
  Widget _buildSaveBtn() {
    return SizedBox(
      width: double.infinity,
      child: GradientTextBtn(
        StringName.intimacyGenerateCharacterAddKeyboard,
        onPressed: () {
          controller.doOnSave(onSaveCallback);
        },
      ),
    );
  }
}

