// Author: fengshengxiong
// Date: 2024/5/11
// Description: 创建播放列表弹框

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/divider_widget.dart';
import 'package:tone_snap/res/themes/app_colors.dart';
import 'package:tone_snap/utils/obj_util.dart';

class CreatePlaylistDialog extends Dialog {
  CreatePlaylistDialog({super.key,this.name, required this.onTap});

  final _textEditingController = TextEditingController();
  final String? name;
  final Function(String name) onTap;

  @override
  Widget build(BuildContext context) {
    _textEditingController.text = name ?? '';
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Material(
        type: MaterialType.transparency,
        child: Center(
          child: IntrinsicHeight(
            child: Container(
              width: 1.sw * 0.72,
              clipBehavior: Clip.antiAlias,
              decoration: BoxDecoration(
                color: const Color(0xFF282A2C),
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Column(
                children: [
                  SizedBox(height: 19.h),
                  Text(
                    'Create  playlist',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 17.sp,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  SizedBox(height: 4.h),
                  Text(
                    'Enter up to 60 characters',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 12.sp,
                    ),
                  ),
                  SizedBox(height: 16.h),
                  Container(
                    height: 30.h,
                    alignment: Alignment.center,
                    margin: EdgeInsets.symmetric(horizontal: 16.w),
                    decoration: BoxDecoration(
                      color: const Color(0xFF1c1c1e),
                      borderRadius: BorderRadius.circular(6).r,
                      border: Border.all(
                        color: const Color(0xff333437),
                        width: 1.w,
                      ),
                    ),
                    child: TextField(
                      maxLines: 1,
                      controller: _textEditingController,
                      textInputAction: TextInputAction.done,
                      keyboardType: TextInputType.text,
                      style: TextStyle(color: Colors.white, fontSize: 15.sp),
                      textAlignVertical: TextAlignVertical.center,
                      maxLength: 60,
                      decoration: InputDecoration(
                        counterText: '',
                        hintText: 'My playlist1',
                        hintStyle: TextStyle(
                          color: const Color(0xFF5a635f),
                          fontSize: 15.sp,
                        ),
                        contentPadding: const EdgeInsets.symmetric(horizontal: 10).w,
                        isCollapsed: true,
                        border: InputBorder.none,
                      ),
                    ),
                  ),
                  SizedBox(height: 16.h),
                  DividerWidget(
                    height: 0.5.h,
                    color: const Color(0xA6545458),
                  ),
                  SizedBox(
                    height: 44.h,
                    child: Row(
                      children: [
                        _optionButton('Cancel', false),
                        Container(
                          width: 0.5.w,
                          height: double.infinity,
                          color: const Color(0xA6545458),
                        ),
                        _optionButton('Confirm', true),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _optionButton(String label, bool isConfirm) {
    return Expanded(
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () {
            if (isConfirm) {
              if (ObjUtil.isNotEmpty(_textEditingController.text)) {
                Get.back();
                onTap(_textEditingController.text);
              }
            } else {
              Get.back();
            }
          },
          child: SizedBox(
            height: double.infinity,
            child: Center(
              child: Text(
                label,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: isConfirm ? seedColor : Colors.white,
                  fontSize: 17.sp,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
