import 'dart:io';

import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_livestock/presentation/routes/app_pages.dart';

import 'logic.dart';

class ProfilePage extends GetView<ProfileLogic> {
  ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      spacing: 30,
      children: [
        Expanded(
          flex: 1,
          child: Container(
            color: AppColor.blueNormal,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(),
                Container(
                  width: 128.w,
                  height: 128.h,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: AppColor.blueLightActive,
                  ),
                  child: Center(
                    child: CircleAvatar(
                      radius: 64.w,
                      backgroundImage: Assets.images.chicken.provider(),
                    ),
                  ),
                ),
                /*           ObxValue((data) {
                  final status = data.value.status;

                  if (status == ResourceStatus.loading) {
                    return Container(
                      width: 128.w,
                      height: 128.h,
                      child: Center(child: CupertinoActivityIndicator(color: AppColor.greenNormal)),
                    );
                  }

                  if (status == ResourceStatus.error) {
                    return Container(
                      width: 128.w,
                      height: 128.h,
                      child: Center(child: Text('خطا در دریافت اطلاعات')),
                    );
                  }

                  // Default UI
                  return Container(
                    width: 128.w,
                    height: 128.h,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: AppColor.blueLightActive,
                    ),
                    child: Center(
                      child: CircleAvatar(
                        radius: 64.w,
                        backgroundImage: NetworkImage(data.value.data!.image!),
                      ),
                    ),
                  );
                }, controller.userProfile),*/
              ],
            ),
          ),
        ),
        Expanded(
          flex: 3,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            spacing: 16,
            children: [
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  child: userProfileInformation(),
                ),
              ),

              Center(
                child: Wrap(
                  alignment: WrapAlignment.center,
                  spacing: 20,
                  runSpacing: 10,
                  children: [
                    cardActionWidget(
                      title: 'تغییر رمز عبور',
                      selected: true,
                      onPressed: () {
                        Get.bottomSheet(changePasswordBottomSheet(), isScrollControlled: true);
                      },
                      icon: Assets.vec.lockSvg.path,
                    ),
                    cardActionWidget(
                      title: 'خروج',
                      selected: true,
                      color: ColorFilter.mode(Colors.redAccent, BlendMode.srcIn),
                      cardColor: Color(0xFFEFEFEF),
                      textColor: AppColor.redDarkerText,
                      onPressed: () {
                        Get.bottomSheet(exitBottomSheet(), isScrollControlled: true);
                      },
                      icon: Assets.vec.logoutSvg.path,
                    ),
                  ],
                ),
              ),

              SizedBox(height: 100),
            ],
          ),
        ),
      ],
    );
  }

  Container invoiceIssuanceInformation() => Container();

  Widget bankInformationWidget() => Column(
    spacing: 16,
    children: [
      itemList(title: 'نام بانک', content: 'سامان'),
      itemList(title: 'نام صاحب حساب', content: 'رضا رضایی'),
      itemList(title: 'شماره کارت ', content: '54154545415'),
      itemList(title: 'شماره حساب', content: '62565263263652'),
      itemList(title: 'شماره شبا', content: '62565263263652'),
    ],
  );

  Widget userProfileInformation() {
    return Column(
      spacing: 6,
      children: [
        buildRowOnTapped(
          onTap: () {
            Get.bottomSheet(
              userInformationBottomSheet(),
              isScrollControlled: true,
              ignoreSafeArea: false,
            );
          },
          titleWidget: Column(
            spacing: 3,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'اطلاعات هویتی',
                style: AppFonts.yekan16Bold.copyWith(color: AppColor.blueNormal),
              ),
              Container(width: 37.w, height: 1.h, color: AppColor.greenNormal),
            ],
          ),
          valueWidget: Assets.vec.editSvg.svg(
            width: 24.w,
            height: 24.h,
            colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
          ),
        ),
        itemList(
          title: 'نام و نام خانوادگی',
          content: /*item.fullname ??*/ 'نامشخص',
          icon: Assets.vec.userSvg.path,
          hasColoredBox: true,
        ),
        itemList(
          title: 'موبایل',
          content: /* item.mobile ??*/ 'نامشخص',
          icon: Assets.vec.callSvg.path,
        ),
        itemList(
          title: 'کدملی',
          content: /* item.nationalId ?? */ 'نامشخص',
          icon: Assets.vec.tagUserSvg.path,
        ),
        itemList(
          title: 'شماره شناسنامه',
          content: /* item.nationalCode ??*/ 'نامشخص',
          icon: Assets.vec.userSquareSvg.path,
        ),
        itemList(
          title: 'تاریخ تولد',
          content: /*item.birthday?.toJalali.formatCompactDate() ??*/ 'نامشخص',
          icon: Assets.vec.calendarSvg.path,
        ),
        itemList(
          title: 'استان',
          content: /*item.province ??*/ 'نامشخص',
          icon: Assets.vec.pictureFrameSvg.path,
        ),
        itemList(title: 'شهر', content: /* item.city ?? */ 'نامشخص', icon: Assets.vec.mapSvg.path),
      ],
    );
    /* return ObxValue((data) {
      if (data.value.status == ResourceStatus.loading) {
        return LoadingWidget();
      } else if (data.value.status == ResourceStatus.error) {
        return ErrorWidget('خطا در دریافت اطلاعات کاربر');
      } else if (data.value.status == ResourceStatus.success) {
        UserProfile item = data.value.data!;
        return Column(
          spacing: 6,
          children: [
            buildRowOnTapped(
              onTap: () {
                Get.bottomSheet(
                  userInformationBottomSheet(),
                  isScrollControlled: true,
                  ignoreSafeArea: false,
                );
              },
              titleWidget: Column(
                spacing: 3,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'اطلاعات هویتی',
                    style: AppFonts.yekan16Bold.copyWith(color: AppColor.blueNormal),
                  ),
                  Container(width: 37.w, height: 1.h, color: AppColor.greenNormal),
                ],
              ),
              valueWidget: Assets.vec.editSvg.svg(
                width: 24.w,
                height: 24.h,
                colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
              ),
            ),
            itemList(
              title: 'نام و نام خانوادگی',
              content: */ /*item.fullname ??*/ /* 'نامشخص',
              icon: Assets.vec.userSvg.path,
              hasColoredBox: true,
            ),
            itemList(
              title: 'موبایل',
              content:*/ /* item.mobile ??*/ /* 'نامشخص',
              icon: Assets.vec.callSvg.path,
            ),
            itemList(
              title: 'کدملی',
              content:*/ /* item.nationalId ?? */ /*'نامشخص',
              icon: Assets.vec.tagUserSvg.path,
            ),
            itemList(
              title: 'شماره شناسنامه',
              content:*/ /* item.nationalCode ??*/ /* 'نامشخص',
              icon: Assets.vec.userSquareSvg.path,
            ),
            itemList(
              title: 'تاریخ تولد',
              content: */ /*item.birthday?.toJalali.formatCompactDate() ??*/ /* 'نامشخص',
              icon: Assets.vec.calendarSvg.path,
            ),
            itemList(
              title: 'استان',
              content: item.province ?? 'نامشخص',
              icon: Assets.vec.pictureFrameSvg.path,
            ),
            itemList(title: 'شهر', content: item.city ?? 'نامشخص', icon: Assets.vec.mapSvg.path),
          ],
        );
      } else {
        return SizedBox.shrink();
      }
    }, controller.userProfile);*/
  }

  Widget itemList({
    required String title,
    required String content,
    String? icon,
    bool hasColoredBox = false,
  }) => Container(
    padding: EdgeInsets.symmetric(horizontal: 12.h, vertical: 6.h),
    decoration: BoxDecoration(
      color: hasColoredBox ? AppColor.greenLight : Colors.transparent,
      borderRadius: BorderRadius.circular(8),
      border: hasColoredBox
          ? Border.all(width: 0.25, color: AppColor.bgDark)
          : Border.all(width: 0, color: Colors.transparent),
    ),
    child: Row(
      spacing: 4,
      children: [
        if (icon != null)
          Padding(
            padding: const EdgeInsets.only(left: 8.0),
            child: SvgGenImage.vec(icon).svg(
              width: 20.w,
              height: 20.h,
              colorFilter: ColorFilter.mode(AppColor.mediumGreyNormalActive, BlendMode.srcIn),
            ),
          ),
        Text(title, style: AppFonts.yekan12.copyWith(color: AppColor.mediumGreyNormalActive)),
        Spacer(),
        Text(content, style: AppFonts.yekan13.copyWith(color: AppColor.mediumGreyNormalHover)),
      ],
    ),
  );

  Widget cardActionWidget({
    required String title,
    required VoidCallback onPressed,
    required String icon,
    bool selected = false,
    ColorFilter? color,
    Color? cardColor,
    Color? textColor,
  }) {
    return GestureDetector(
      onTap: onPressed,
      child: Column(
        spacing: 4,
        children: [
          Container(
            width: 52,
            height: 52,
            padding: EdgeInsets.all(8),
            decoration: ShapeDecoration(
              color: cardColor ?? AppColor.blueLight,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
            ),
            child: SvgGenImage.vec(icon).svg(
              width: 40,
              height: 40,
              colorFilter:
                  color ??
                  ColorFilter.mode(
                    selected ? AppColor.blueNormal : AppColor.whiteLight,
                    BlendMode.srcIn,
                  ),
            ),
          ),
          SizedBox(height: 2),
          Text(
            title,
            style: AppFonts.yekan10.copyWith(
              color: textColor ?? (selected ? AppColor.blueNormal : AppColor.blueLightActive),
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget userInformationBottomSheet() {
    return BaseBottomSheet(
      height: 750.h,
      child: SingleChildScrollView(
        child: Column(
          spacing: 8,
          children: [
            Text(
              'ویرایش اطلاعات هویتی',
              style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover),
            ),

            Container(
              padding: EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(color: AppColor.darkGreyLight, width: 1),
              ),
              child: Column(
                spacing: 12,
                children: [
                  RTextField(
                    controller: controller.nameController,
                    label: 'نام',
                    borderColor: AppColor.darkGreyLight,
                    filledColor: AppColor.bgLight,
                    filled: true,
                  ),
                  RTextField(
                    controller: controller.lastNameController,
                    label: 'نام خانوادگی',
                    borderColor: AppColor.darkGreyLight,
                    filledColor: AppColor.bgLight,
                    filled: true,
                  ),
                  RTextField(
                    controller: controller.nationalCodeController,
                    label: 'شماره شناسنامه',
                    borderColor: AppColor.darkGreyLight,
                    filledColor: AppColor.bgLight,
                    filled: true,
                  ),
                  RTextField(
                    controller: controller.nationalIdController,
                    label: 'کد ملی',
                    borderColor: AppColor.darkGreyLight,
                    filledColor: AppColor.bgLight,
                    filled: true,
                  ),

                  ObxValue((data) {
                    return RTextField(
                      controller: controller.birthdayController,
                      label: 'تاریخ تولد',
                      initText: data.value?.formatCompactDate() ?? '',
                      borderColor: AppColor.darkGreyLight,
                      filledColor: AppColor.bgLight,
                      filled: true,
                      onTap: () {},
                    );
                  }, controller.birthDate),

                  SizedBox(),
                ],
              ),
            ),
            SizedBox(),

            Container(
              padding: EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(color: AppColor.darkGreyLight, width: 1),
              ),
              child: Column(
                spacing: 8,
                children: [
                  Text(
                    'عکس پروفایل',
                    style: AppFonts.yekan16Bold.copyWith(color: AppColor.blueNormal),
                  ),
                  ObxValue((data) {
                    return Container(
                      width: Get.width,
                      height: 270,
                      decoration: BoxDecoration(
                        color: AppColor.lightGreyNormal,
                        borderRadius: BorderRadius.circular(8),
                        border: Border.all(width: 1, color: AppColor.blackLight),
                      ),
                      child: Center(
                        child: data.value == null
                            ? Padding(
                                padding: const EdgeInsets.fromLTRB(30, 10, 10, 30),
                                child: Image.network(''),
                              )
                            : Image.file(File(data.value!.path), fit: BoxFit.cover),
                      ),
                    );
                  }, controller.selectedImage),

                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      RElevated(
                        text: 'گالری',
                        width: 150.w,
                        height: 40.h,
                        textStyle: AppFonts.yekan20.copyWith(color: Colors.white),
                        onPressed: () async {
                          controller.selectedImage.value = await controller.imagePicker.pickImage(
                            source: ImageSource.gallery,
                            imageQuality: 60,
                            maxWidth: 1080,
                            maxHeight: 720,
                          );
                        },
                      ),
                      SizedBox(width: 16),
                      ROutlinedElevated(
                        text: 'دوربین',
                        width: 150.w,
                        height: 40.h,
                        textStyle: AppFonts.yekan20.copyWith(color: AppColor.blueNormal),
                        onPressed: () async {
                          controller.selectedImage.value = await controller.imagePicker.pickImage(
                            source: ImageSource.camera,
                            imageQuality: 60,
                            maxWidth: 1080,
                            maxHeight: 720,
                          );
                        },
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Row(
              spacing: 16,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ObxValue((data) {
                  return RElevated(
                    height: 40.h,
                    text: 'ویرایش',
                    isLoading: data.value,
                    onPressed: () async {
                      await controller.updateUserProfile();
                      controller.getUserProfile();
                      Get.back();
                    },
                  );
                }, controller.isOnLoading),
                ROutlinedElevated(
                  height: 40.h,
                  text: 'انصراف',
                  borderColor: AppColor.blueNormal,
                  onPressed: () {
                    Get.back();
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  /*  Widget _provinceWidget() {
    return Obx(() {
      return OverlayDropdownWidget<IranProvinceCityModel>(
        items: controller.rootLogic.provinces,
        onChanged: (value) {
          controller.selectedProvince.value = value;
        },
        selectedItem: controller.selectedProvince.value,
        itemBuilder: (item) => Text(item.name ?? 'بدون نام'),
        labelBuilder: (item) => Text(item?.name ?? 'انتخاب استان'),
      );
    });
  }

  Widget _cityWidget() {
    return ObxValue((data) {
      return OverlayDropdownWidget<IranProvinceCityModel>(
        items: data,
        onChanged: (value) {
          controller.selectedCity.value = value;
        },
        selectedItem: controller.selectedCity.value,
        itemBuilder: (item) => Text(item.name ?? 'بدون نام'),
        labelBuilder: (item) => Text(item?.name ?? 'انتخاب شهر'),
      );
    }, controller.cites);
  }*/

  Widget changePasswordBottomSheet() {
    return BaseBottomSheet(
      height: 400.h,
      child: SingleChildScrollView(
        child: Form(
          key: controller.formKey,
          child: Column(
            spacing: 8,
            children: [
              Text(
                'تغییر رمز عبور',
                style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover),
              ),
              SizedBox(),
              RTextField(
                controller: controller.oldPasswordController,
                hintText: 'رمز عبور قبلی',
                borderColor: AppColor.darkGreyLight,
                filledColor: AppColor.bgLight,
                filled: true,
                validator: (value) {
                  /*  if (value == null || value.isEmpty) {
                    return 'رمز عبور را وارد کنید';
                  } else if (controller.userProfile.value.data?.password != value) {
                    return 'رمز عبور  صحیح نیست';
                  }*/
                  return null;
                },
              ),
              RTextField(
                controller: controller.newPasswordController,
                hintText: 'رمز عبور جدید',
                borderColor: AppColor.darkGreyLight,
                filledColor: AppColor.bgLight,
                filled: true,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'رمز عبور را وارد کنید';
                  } else if (value.length < 6) {
                    return 'رمز عبور باید بیش از 6 کارکتر باشد.';
                  }
                  return null;
                },
              ),
              RTextField(
                controller: controller.retryNewPasswordController,
                hintText: 'تکرار رمز عبور جدید',
                borderColor: AppColor.darkGreyLight,
                filledColor: AppColor.bgLight,
                filled: true,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'رمز عبور را وارد کنید';
                  } else if (value.length < 6) {
                    return 'رمز عبور باید بیش از 6 کارکتر باشد.';
                  } else if (controller.newPasswordController.text != value) {
                    return 'رمز عبور جدید یکسان نیست';
                  }
                  return null;
                },
              ),

              SizedBox(),

              Row(
                spacing: 16,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  RElevated(
                    height: 40.h,
                    text: 'ویرایش',
                    onPressed: () async {
                      if (controller.formKey.currentState?.validate() != true) {
                        return;
                      }
                      await controller.updatePassword();
                      controller.getUserProfile();
                      controller.clearPasswordForm();
                      Get.back();
                    },
                  ),
                  ROutlinedElevated(
                    height: 40.h,
                    text: 'انصراف',
                    borderColor: AppColor.blueNormal,
                    onPressed: () {
                      Get.back();
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget exitBottomSheet() {
    return BaseBottomSheet(
      height: 220.h,
      child: SingleChildScrollView(
        child: Form(
          key: controller.formKey,
          child: Column(
            spacing: 8,
            children: [
              Text('خروج', style: AppFonts.yekan16Bold.copyWith(color: AppColor.error)),
              SizedBox(),
              Text(
                'آیا مطمئن هستید که می‌خواهید از حساب کاربری خود خارج شوید؟',
                textAlign: TextAlign.center,
                style: AppFonts.yekan16Bold.copyWith(color: AppColor.textColor),
              ),

              SizedBox(),

              Row(
                spacing: 16,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  RElevated(
                    height: 40.h,
                    text: 'خروج',
                    backgroundColor: AppColor.error,
                    onPressed: () async {
                      await controller.rootLogic.tokenService
                          .deleteModuleTokens(Module.liveStocks)
                          .then((value) {
                            Get.back();
                            Get.offAllNamed(LiveStockRoutes.auth, arguments: Module.chicken);
                          });
                    },
                  ),
                  ROutlinedElevated(
                    height: 40.h,
                    text: 'انصراف',
                    borderColor: AppColor.blueNormal,
                    onPressed: () {
                      Get.back();
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
