import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/services/auth/user_profile_manager.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
import 'package:yumi/services/general/sc_app_general_manager.dart';
import 'package:yumi/modules/index/main_route.dart';

class SCProfilePage extends StatefulWidget {
  bool isFromMe = false;
  String userId = "";

  SCProfilePage(this.isFromMe, this.userId);

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<SCProfilePage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<SocialChatUserProfileManager>(
      builder: (context, ref, child) {
        List<PersonPhoto> items = [];
        if (widget.isFromMe) {
          items =
              (AccountStorage().getCurrentUser()?.userProfile?.personalPhotos ??
                      [])
                  .where((t) => t.status == 1)
                  .toList();
        } else {
          items = ref.userProfile?.personalPhotos ?? [];
          items =
              (ref.userProfile?.personalPhotos ?? [])
                  .where((t) => t.status == 1)
                  .toList();
        }
        return SingleChildScrollView(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 15.w),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 5.w),
                GridView.builder(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3, // 每行2个
                    childAspectRatio: 1, // 宽高比
                    mainAxisSpacing: 8,
                    crossAxisSpacing: 8,
                  ),
                  itemCount: items.length,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      child: netImage(
                        url: items[index].url ?? "",
                        width: double.infinity,
                        height: double.infinity,
                        defaultImg: "sc_images/general/sc_icon_loading.webp",
                        borderRadius: BorderRadius.circular(12.w),
                      ),
                      onTap: () {
                        List<String> ims = [];
                        for (var iv in items) {
                          if ((iv.url?.isNotEmpty ?? false)) {
                            ims.add(iv.url ?? "");
                          }
                        }
                        String encodedUrls = Uri.encodeComponent(
                          jsonEncode(ims),
                        );
                        SCNavigatorUtils.push(
                          context,
                          "${SCMainRoute.imagePreview}?imageUrls=$encodedUrls&initialIndex=$index",
                        );
                      },
                    );
                  },
                ),
                SizedBox(height: 5.w),
                text(
                  SCAppLocalizations.of(context)!.personal2,
                  fontSize: 14.sp,
                  fontWeight: FontWeight.bold,
                  textColor: Colors.white,
                ),
                SizedBox(height: 15.w),
                _item2(
                  SCAppLocalizations.of(context)!.country,
                  widget.isFromMe
                      ? AccountStorage()
                              .getCurrentUser()
                              ?.userProfile
                              ?.countryName ??
                          ""
                      : ref.userProfile?.countryName ?? "",
                ),
                SizedBox(height: 10.w),
                _item(SCAppLocalizations.of(context)!.language, "English"),
                SizedBox(height: 10.w),
                _item(
                  SCAppLocalizations.of(context)!.dateOfBirth,
                  "${widget.isFromMe ? AccountStorage().getCurrentUser()?.userProfile?.bornYear ?? "" : ref.userProfile?.bornYear}-${widget.isFromMe ? AccountStorage().getCurrentUser()?.userProfile?.bornMonth ?? "" : ref.userProfile?.bornMonth}-${widget.isFromMe ? AccountStorage().getCurrentUser()?.userProfile?.bornDay ?? "" : ref.userProfile?.bornDay}",
                ),
                SizedBox(height: 10.w),
                _item(
                  SCAppLocalizations.of(context)!.bio,
                  widget.isFromMe
                      ? AccountStorage()
                              .getCurrentUser()
                              ?.userProfile
                              ?.autograph ??
                          ""
                      : ref.userProfile?.autograph ?? "",
                ),
                SizedBox(height: 10.w),
                _item(
                  SCAppLocalizations.of(context)!.hobby,
                  widget.isFromMe
                      ? AccountStorage().getCurrentUser()?.userProfile?.hobby ??
                          ""
                      : ref.userProfile?.hobby ?? "",
                ),
                SizedBox(height: 62.w),
              ],
            ),
          ),
        );
      },
    );
  }

  _item(String name, String value) {
    return Row(
      children: [
        SizedBox(
          width: 100.w,
          child: text(name, textColor: Color(0xffB1B1B1), fontSize: 13.sp),
        ),
        Expanded(
          child: text(
            value,
            fontSize: 13.sp,
            textColor: Colors.white,
            maxLines: 2,
          ),
        ),
      ],
    );
  }

  _item2(String name, String value) {
    return Row(
      children: [
        SizedBox(
          width: 100.w,
          child: text(name, textColor: Color(0xffB1B1B1), fontSize: 13.sp),
        ),
        netImage(
          url:
              Provider.of<SCAppGeneralManager>(
                context,
                listen: false,
              ).findCountryByName(value)?.nationalFlag ??
              "",
          borderRadius: BorderRadius.all(Radius.circular(3.w)),
          width: 19.w,
          height: 14.w,
        ),
        SizedBox(width: 3.w),
        Expanded(
          child: text(
            value,
            fontSize: 13.sp,
            textColor: Colors.white,
            maxLines: 2,
          ),
        ),
      ],
    );
  }


}
