
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:location/module/track/track_dialog/track_overlapping_avatars.dart';
import 'package:location/utils/common_expand.dart';

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

class TrackDailyReportDialog {
  static void show({
    VoidCallback? cancelOnTap,
    VoidCallback? confirmOnTap,
    List<UserInfo>? trackDailyList,
  }) {
    Get.dialog(
        barrierDismissible: false,
        SimpleDialog(
          titlePadding: EdgeInsets.zero,
          contentPadding: EdgeInsets.zero,
          insetPadding: EdgeInsets.zero,
          backgroundColor:Colors.transparent,
          children: [
            TrackDailyReportTipView(
                cancelOnTap: () {
                  Get.back();
                  cancelOnTap!();
                },
                confirmOnTap: confirmOnTap,
              trackDailyList: trackDailyList,
            )
          ],
        )
    );
  }
}

class TrackDailyReportTipView extends StatefulWidget {
  final VoidCallback? cancelOnTap;
  final VoidCallback? confirmOnTap;
  final List<UserInfo>? trackDailyList;


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

  @override
  State<TrackDailyReportTipView> createState() => _TrackDailyReportTipViewState();
}

class _TrackDailyReportTipViewState extends State<TrackDailyReportTipView> {

  late List<String> avatarUrls = [];

  @override
  void initState() {
    super.initState();
    print("widget.trackDailyListfsdfsdf---${widget.trackDailyList}");
    avatarUrls = extractUrls(widget.trackDailyList);//widget.trackDailyList!.map((item) => item.avatar ?? "").whereType<String>().toList();
  }

  List<String> extractUrls(List<UserInfo>? trackDailyList) {
    // 若列表为 null，则返回空列表
    if (trackDailyList == null) return [];

    // 使用 map 操作提取 url 字段，并过滤掉可能的 null 值
    return trackDailyList
        .map((item) => item.avatar) // 假设 UserInfo 类中有 url 字段
        .whereType<String>()     // 过滤掉可能的 null 值
        .toList();
  }

  //计算出头像总共的宽度
  double _calculateTheTotalWithAvatar(List<String> avatarUrls) {
    var resultTotalW = 38.w;
    if (avatarUrls.length > 5) {
      resultTotalW = (avatarUrls.length - 1) * 29.w + 9.w;
    } else if (avatarUrls.length > 1) {
      resultTotalW = (avatarUrls.length) * 29.w + 9.w;
    }
    return resultTotalW;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      width: 1.sw,
      margin: EdgeInsets.symmetric(horizontal: 43.w),
      child: IntrinsicHeight(
        child: Column(
          children: [
            Container(
              decoration: BoxDecoration(
                  color: Colors.transparent,
                  image: DecorationImage(
                    image: Assets.images.iconTrackDailyReport.provider(),
                    fit: BoxFit.fill,
                  )
              ),
              child: Column(
                children: [
                  SizedBox(
                    height: 146.w,
                  ),
                  Container(
                    height: 38.w,
                    width: _calculateTheTotalWithAvatar(avatarUrls),
                    child: OverlappingAvatars(
                      avatarUrls: avatarUrls,
                      size: 38.w,
                      overlap: 0.763,
                      borderColor: "#A287FF".color,
                      maxDisplay: 5,
                    ),
                  ),
                  SizedBox(
                    height: 8.w,
                  ),
                  Text("昨日轨迹报告",
                      style: TextStyle(
                          fontSize: 13.sp,
                          color: '#57577E'.color,
                          fontWeight: FontWeight.w700)
                  ),
                  SizedBox(
                    height: 46.w,
                  ),
                  GestureDetector(
                    onTap: () {
                      Get.back();
                      widget.confirmOnTap!();
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.centerLeft, // 90度相当于从左到右
                          end: Alignment.centerRight,
                          colors: [
                            Color(0xFF7B7DFF), // #7B7DFF
                            Color(0xFF6365FF), // #6365FF
                          ],
                          stops: [0.0, 1.0],
                          // 从0%到100%
                        ),
                        borderRadius: BorderRadius.circular(40.w / 2.0),
                      ),
                      margin: EdgeInsets.symmetric(horizontal: 20.w),
                      height: 40.w,
                      alignment: Alignment.center,
                      child: Text("立即查看",
                          style: TextStyle(
                              fontSize: 14.sp,
                              color: '#FFFFFF'.color,
                              fontWeight: FontWeight.w500)
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 10.w,
                  ),
                  GestureDetector(
                    onTap: widget.cancelOnTap,
                    child: Container(
                      height: 20.w,
                      child: Text("下次再说",
                          style: TextStyle(
                              fontSize: 11.sp,
                              color: '#898996'.color,
                              fontWeight: FontWeight.w500)
                      ),
                    ),
                  ),

                  SizedBox(
                    height: 12.w,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}