import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:location/data/api/response/track_daily_summary_response.dart';
import 'package:location/data/bean/user_info.dart';
import 'package:location/data/repositories/account_repository.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';
import 'package:location/utils/date_util.dart';

import '../../../data/bean/track_summary.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/fonts.gen.dart';
import '../../../utils/common_style.dart';
import '../../../widget/dashed_line.dart';
import '../../../widget/rich_text_replace.dart';

class TrackShareView extends StatelessWidget {
  final TrackDailySummaryResponse trackDailySummary;
  final UserInfo userInfo;
  final int day;

  const TrackShareView(this.day, this.userInfo, this.trackDailySummary,
      {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        gradient: LinearGradient(
            colors: ['#C8C0FF'.color, '#FFFFFF'.color, '#B7ACFF'.color],
            begin: Alignment.bottomLeft,
            end: Alignment.topRight),
      ),
      child: IntrinsicHeight(
        child: Stack(
          children: [
            buildShareTitleView(),
            buildRobotView(),
            Column(
              children: [
                SizedBox(height: 129.w),
                buildShareContentView(),
              ],
            ),
            _buildShareAvatarView(),
          ],
        ),
      ),
    );
  }

  Widget buildShareContentView() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 24.w),
      child: Column(
        children: [
          buildContent1View(),
          Container(
            color: ColorName.white,
            margin: EdgeInsets.symmetric(horizontal: 14.w),
            child: DashedLine(
              height: 1.w,
              color: '#CCCCCC'.color,
              dashWidth: 3.w,
              dashSpace: 3.w,
            ),
          ),
          buildContent2View(),
          Visibility(
              visible: trackDailySummary.stayLongest == null,
              child: SizedBox(height: 80.w)),
          Visibility(
              visible: !trackDailySummary.showTrackSituation,
              child: SizedBox(height: 80.w)),
          SizedBox(height: 96.w),
          buildAppLogoView(),
          SizedBox(height: 16.w),
        ],
      ),
    );
  }

  Widget buildAppLogoView() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Assets.images.iconLogo.image(width: 28.w, height: 28.w),
        SizedBox(width: 6.w),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(Platform.isIOS? StringName.appNameIos : StringName.appName,
                style: TextStyle(
                    fontSize: 12.sp,
                    fontWeight: FontWeight.bold,
                    color: ColorName.black)),
            SizedBox(height: 2.w),
            Text(StringName.trackStayShareLogoDesc,
                style: TextStyle(fontSize: 9.sp, color: '#74839C'.color))
          ],
        )
      ],
    );
  }

  Widget buildContent2View() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
      decoration: BoxDecoration(
          color: ColorName.white, borderRadius: BorderRadius.circular(14.w)),
      child: Column(
        children: [
          _buildSituationItem(
              Assets.images.iconSharePhone.provider(),
              StringName.trackDailySummaryPhone,
              trackDailySummary.phoneSituation,
              isShowDivider: trackDailySummary.stayLongest != null),
          _buildSituationItem(Assets.images.iconShareStay.provider(),
              StringName.trackDailySummaryStay, trackDailySummary.stayLongest,
              isShowDivider: trackDailySummary.showTrackSituation &&
                  trackDailySummary.trackSituation?.isNotEmpty == true),
          _buildDailyTrack()
        ],
      ),
    );
  }

  Widget _buildDailyTrack() {
    if (trackDailySummary.trackSituation?.isNotEmpty == true) {
      return SizedBox(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(children: [
              Assets.images.iconShareTrackDetail
                  .image(width: 15.w, height: 15.w),
              SizedBox(width: 4.w),
              Text(StringName.trackDailySummarytrack,
                  style: TextStyle(
                      fontSize: 13.sp,
                      color: '#333333'.color,
                      fontWeight: FontWeight.bold)),
            ]),
            SizedBox(height: 8.w),
            Text(
              trackDailySummary.trackSituation ?? '',
              style: TextStyle(fontSize: 11.sp, color: '#666666'.color),
            ),
            SizedBox(height: 12.w),
          ],
        ),
      );
    }
    return SizedBox.shrink();
  }

  Widget _buildSituationItem(
      ImageProvider icon, String title, TrackSummary? summary,
      {bool isShowDivider = true}) {
    if (summary == null) {
      return SizedBox.shrink();
    }
    return SizedBox(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(children: [
            Image(image: icon, width: 15.w, height: 15.w),
            SizedBox(width: 4.w),
            Text(title,
                style: TextStyle(
                    fontSize: 13.sp,
                    color: '#333333'.color,
                    fontWeight: FontWeight.bold)),
          ]),
          SizedBox(height: 8.w),
          RichTextReplace(
              text: summary.text,
              items: summary.items,
              defaultStyle: TextStyle(fontSize: 11.sp, color: '#666666'.color),
              replacedStyle: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 11.sp,
                  color: '#333333'.color)),
          SizedBox(height: 16.w),
          Visibility(
            visible: isShowDivider,
            child: Container(
              width: double.infinity,
              height: 1.w,
              color: '#EEEEEE'.color,
            ),
          ),
          Visibility(visible: isShowDivider, child: SizedBox(height: 16.w)),
        ],
      ),
    );
  }

  Widget buildContent1View() {
    return Container(
      decoration: BoxDecoration(
          color: ColorName.white,
          borderRadius: BorderRadius.circular(14.w),
          border: Border.all(color: ColorName.white, width: 2.w),
          image: DecorationImage(
              image: Assets.images.bgTrackReportUser.provider())),
      width: double.infinity,
      height: 95.w,
      child: Stack(
        children: [
          Positioned(
            top: 0,
            bottom: 0,
            right: 32.w,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(DateUtil.getDayOfMonth(day),
                    style: TextStyle(
                        fontFamily: FontFamily.oppoSans,
                        fontSize: 36.w,
                        fontWeight: FontWeight.bold,
                        height: 1)),
                SizedBox(height: 4.w),
                Row(
                  children: [
                    Text(
                        '${DateUtil.fromMillisecondsSinceEpoch("yyyy/MM", day)} ${DateUtil.getWeekday(day)}',
                        style: TextStyle(
                            fontSize: 11.sp,
                            color: '#666666'.color,
                            height: 1)),
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _buildShareAvatarView() {
    return Positioned(
      left: 54.w,
      top: 118.w,
      child: Column(
        children: [
          Container(
            width: 68.w,
            height: 68.w,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              border: Border.all(color: '#A362FF'.color, width: 2.w),
            ),
            child: buildCustomAvatarView(
              size: double.infinity,
              avatar: userInfo.avatar ?? '',
            ),
          ),
          SizedBox(height: 6.w),
          Text(getUserName(),
              style: TextStyle(
                  fontSize: 12.sp,
                  color: '#333333'.color,
                  fontWeight: FontWeight.w500))
        ],
      ),
    );
  }

  String getUserName() {
    String phone;
    if (userInfo.isMine == true) {
      phone = AccountRepository.getInstance().loginPhoneNum.value ??
          userInfo.phoneNumber;
    } else {
      phone = userInfo.phoneNumber;
    }
    if (phone.length > 4) {
      phone = phone.substring(phone.length - 4);
    }
    return '${StringName.mineAccountLoggedDesc}$phone';
  }

  Widget buildRobotView() {
    return Positioned(
        top: 8.w,
        right: 6.w,
        child: Assets.images.imgTrackReportRobot
            .image(width: 150.w, height: 150.w));
  }

  Widget buildShareTitleView() {
    return Positioned(
      left: 23.w,
      top: 31.w,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Assets.images.imgTrackReportTitle.image(height: 57.w),
          Row(
            children: [
              if (trackDailySummary.showTrackSituation &&
                  trackDailySummary.trackSituation?.isNotEmpty == true)
                _buildTitle(StringName.trackDailySummarytrack),
              if (trackDailySummary.phoneSituation != null)
                _buildTitle(StringName.trackDailySummaryPhone),
              if (trackDailySummary.stayLongest != null)
                _buildTitle(StringName.trackStayLongestPlace),
            ],
          )
        ],
      ),
    );
  }

  Widget _buildTitle(String title) {
    return Container(
        margin: EdgeInsets.only(right: 6.w),
        padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 4.w),
        decoration: BoxDecoration(
            color: '#A362FF'.color, borderRadius: BorderRadius.circular(100.w)),
        child: Text(title,
            style:
                TextStyle(fontSize: 10.sp, color: ColorName.white, height: 1)));
  }
}
