import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:location/module/track/track_dialog/track_choose_friend_item.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';

import '../../../data/bean/user_info.dart';
import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';


class TrackChooseFriendDialog {
  static const String _tag = 'TrackChooseFriendDialog';

  static void show({List<UserInfo>? trackDailyList,SelectAcceptCallback? onSelectItem}) {
    SmartDialog.show(
        builder: (_) => TrackChooseFriendView(confirmOnTap: () {},trackDailyList: trackDailyList,onSelectItem: onSelectItem,),
        tag: _tag,
        clickMaskDismiss: false);
  }

  static void dismiss() {
    SmartDialog.dismiss(tag: _tag);
  }
}

class TrackChooseFriendView extends StatefulWidget {
  final VoidCallback? cancelOnTap;
  final VoidCallback? confirmOnTap;
  final List<UserInfo>? trackDailyList;
  final SelectAcceptCallback? onSelectItem;

  const TrackChooseFriendView({
    super.key,
    this.cancelOnTap,
    required this.confirmOnTap,
    this.trackDailyList,
    this.onSelectItem
  });

  @override
  State<TrackChooseFriendView> createState() => _TrackChooseFriendViewState();
}


class _TrackChooseFriendViewState extends State<TrackChooseFriendView> {

  UserInfo selectDailyDialogsInfo = UserInfo(phoneNumber: "0", id: '0');

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if ((widget.trackDailyList ?? []).isNotEmpty) {
      selectDailyDialogsInfo = (widget.trackDailyList ?? []).first;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 274.w,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: Colors.white, width: 3),
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [
            Color(0xFFE4E4FF), // 对应 #E4E4FF
            Colors.white,     // 对应 #FFF
          ],
          stops: [0.0, 0.1932], // 对应 0% 和 19.32%
        ),
      ),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
              top: 16.w,
              right: 16.w,
              child: GestureDetector(
                onTap: onCloseClick,
                child: Assets.images.iconDialogClose2
                    .image(width: 20.w, height: 20.w),
              )),
          buildAccountReplaceContent(),
        ],
      ),
    );
  }

  Widget buildAccountReplaceContent() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: IntrinsicHeight(
        child: Column(
          children: [
            SizedBox(height: 24.w),
            Container(
              alignment: Alignment.centerLeft,
              width: double.infinity,
              child: Text("选择任意好友",
                  style: TextStyle(fontSize: 17.sp, color: '#333333'.color,fontWeight: FontWeight.bold)),
            ),
            SizedBox(height: 20.w),
            Container(
              height: 231.w,
              child: _buildFriendList(),
            ),
            GestureDetector(
              onTap: onKnowClick,
              child: Container(
                width: 229.w,
                height: 43.w,
                decoration: BoxDecoration(
                  color: ColorName.colorPrimary,
                  borderRadius: BorderRadius.circular(100.r),
                ),
                child: Center(
                  child: Text("立即查看",
                      style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white,
                          fontWeight: FontWeight.w500)),
                ),
              ),
            ),
            SizedBox(height: 16.w),
          ],
        ),
      ),
    );
  }

  Widget _buildFriendList() {
    return ListView.builder(
        padding: EdgeInsets.only(top: 0, bottom: 100.w),
        itemBuilder: (_, index) {
          UserInfo itemInfo = (widget.trackDailyList ?? [])[index];

          return createUserItem(itemInfo,itemInfo.id == selectDailyDialogsInfo.id, onSelectItem: (selectInfo) {
            selectDailyDialogsInfo = selectInfo;
            setState(() {
            });
          });
        },
        itemCount: (widget.trackDailyList ?? []).length);
  }

  void onCloseClick() {
    TrackChooseFriendDialog.dismiss();
  }

  void onKnowClick() {
    widget.onSelectItem!(selectDailyDialogsInfo);
    TrackChooseFriendDialog.dismiss();
  }
}
