import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smartledz_wifi_test/widgets/animations/animation_for_rotation.dart';

class Modal {

  // 构建模态框标题
  static Widget buildTitle({
    required String title,
    double? size,
  }){
    return Padding(
      padding: EdgeInsets.only(bottom: 5.sp),
      child: Center(
        child: Text(
          title,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: size ?? 15.sp,
            color: Colors.black,
          )
        ),
      ),
    );
  }

  // 构建文字内容
  static Widget buildContent({
    required String content,
    TextAlign? textAlign,
  }){
    return Text(
      content,
      textAlign: textAlign,
      style: TextStyle(
        fontSize: 15.sp,
        color: const Color(0xAA000000)
      )
    );
  }

  // 构建带边框的输入框
  static Widget buildInputForBorder({
    String? initValue,
    String? hintText,
    List<TextInputFormatter>? inputFormatters,
    TextEditingController? controller,
    void Function(String)? onChanged,
  }){
    return Container(
      height: 40.sp,
      margin: EdgeInsets.symmetric(vertical: 15.sp),
      decoration: BoxDecoration(
        border: Border.all(
          color: const Color(0xAA000000),
          width: .5
        )
      ),
      child: TextFormField(
        controller: controller,
        initialValue: initValue,
        onChanged: onChanged,
        cursorColor: const Color(0xFF00AAFF),
        decoration: InputDecoration(
          hintText: hintText,
          hintStyle: TextStyle(
            fontSize: 15.sp,
            color: Colors.grey.shade500
          ),
          border: const OutlineInputBorder(borderSide: BorderSide.none),
          contentPadding: EdgeInsets.symmetric(horizontal: 10.sp)
        ),
        inputFormatters: inputFormatters,
        style: TextStyle(fontSize: 15.sp),
      ),
    );
  }

  // 构建两个一组的按钮
  static Widget buildButtonForTwo({
    required String leftBtnTitle,
    required String rightBtnTitle,
    void Function()? onLeftBtnTap,
    void Function()? onRightBtnTap,
  }){
    return Row(
      children: [
        GestureDetector(
          onTap: onLeftBtnTap,
          child: Container(
            color: const Color(0xFFAAAAAA),
            padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 10.w),
            child: Text(leftBtnTitle, style: TextStyle(color: Colors.white, fontSize: 15.sp)),
          ),
        ),
        Expanded(
          child: Align(
            alignment: Alignment.centerRight,
            child: GestureDetector(
              onTap: onRightBtnTap,
              child: Container(
                color: const Color(0xFF00AAFF),
                padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 10.w),
                child: Text(rightBtnTitle, style: TextStyle(color: Colors.white, fontSize: 15.sp)
                ),
              ),
            ),
          ),
        )
      ],
    );
  }

  // 构建自撑宽度的按钮
  static Widget buildButtonForFull({
    required String title,
    void Function()? onTap,
  }){
    return GestureDetector(
      onTap: onTap,
      child: Container(
        alignment: Alignment.center,
        color: const Color(0xFF00AAFF),
        padding: EdgeInsets.symmetric(vertical: 8.h),
        child: Text(title, style: TextStyle(color: Colors.white, fontSize: 15.sp)
        ),
      ),
    );
  }

  // 打开并显示模态框
  static void open({
    required BuildContext context,
    required Widget Function(BuildContext) builder,
    double? width,
    EdgeInsetsGeometry? padding,
    bool disableShadeClose = false, // 禁用遮罩点击关闭
    bool disableReturnKey = false, // 禁用返回键
  }){
    showDialog(
      barrierDismissible: !disableShadeClose,
      context: context,
      builder: (BuildContext context){
        Widget child = UnconstrainedBox(
          child: Container(
            width: width ?? 300.w,
            color: Colors.white,
            padding: padding ?? EdgeInsets.all(15.w),
            child: builder(context),
          ),
        );
        if(disableReturnKey == true){
          return WillPopScope(child: child, onWillPop: () async => false);
        }
        return child;
      }
    );
  }

  // 打开加载遮罩层
  static void openLoading({
    required BuildContext context,
    required String tip,
  }){
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context){
        return WillPopScope(
          onWillPop: () async => false,
          child: UnconstrainedBox(
            child: Row(
              children: [
                AnimationForRotation(
                  duration: const Duration(milliseconds: 400),
                  child: Icon(Icons.refresh, color: const Color(0xFF00AAFF), size: 45.sp),
                ),
                SizedBox(width: 10.sp),
                Text(tip, style: TextStyle(color: Colors.white, fontSize: 23.sp, fontWeight: FontWeight.bold))
              ],
            ),
          ),
        );
      }
    );
  }

  // Toast消息提示
  static void toast({
    required String msg
  }){

  }
}