import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/models/response/user_profile/user_profile.dart';
import 'package:rasadyar_chicken/presentation/widget/list_row_item.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class ProfilePage extends GetView<ProfileLogic> {
  const 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: Assets.vec.userSvg.svg(
                      width: 64.w,
                      height: 64.h,
                      colorFilter: ColorFilter.mode(AppColor.whiteLight, BlendMode.srcIn),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Expanded(
          flex: 3,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            spacing: 16,
            children: [
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  child: userProfileInformation(),
                ),
              ),

              ObxValue((data) {
                return Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Wrap(
                    spacing: 20,
                    runSpacing: 10,
                    children: [
                      cardActionWidget(
                        title: 'اطلاعات کاربری',
                        onPressed: () {
                          data.value = 0;
                        },
                        icon: Assets.vec.profileUserSvg.path,
                        selected: data.value == 0,
                      ),

                      cardActionWidget(
                        title: 'تغییر رمز عبور',
                        selected: true,
                        onPressed: () {
                          Get.bottomSheet(changePasswordBottomSheet(), isScrollControlled: true);
                        },
                        icon: Assets.vec.lockSvg.path,
                      ),
                      /*cardActionWidget(
                        title: 'اطلاعات بانکی',
                        onPressed: () {
                          data.value = 1;
                        },
                        icon: Assets.vec.informationSvg.path,
                        selected: data.value == 1,
                      ),
                      cardActionWidget(
                        title: 'اطلاعات \nصدور فاکتور',
                        onPressed: () {
                          data.value = 2;
                        },
                        icon: Assets.vec.receiptDiscountSvg.path,
                        selected: data.value == 2,
                      ),*/
                    ],
                  ),
                );
              }, controller.selectedInformationType),

              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 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);
              },
              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.blueLight : Colors.transparent,
      border: hasColoredBox
          ? Border.all(width: 1, color: AppColor.blueLightHover)
          : 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,
              height: 20,
              colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
            ),
          ),
        Text(title, style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal)),
        Spacer(),
        Text(content, style: AppFonts.yekan13.copyWith(color: AppColor.darkGreyNormalHover)),
      ],
    ),
  );

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

  Widget userInformationBottomSheet() {
    return BaseBottomSheet(
      height: 500.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: [_provinceWidget(), _cityWidget()]),
            ),*/
            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(),

                  Row(
                    spacing: 16,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      RElevated(
                        height: 40.h,
                        text: 'ویرایش',
                        onPressed: () async {
                          await controller.updateUserProfile();
                          controller.getUserProfile();
                          Get.back();
                        },
                      ),
                      ROutlinedElevated(
                        height: 40.h,
                        text: 'انصراف',
                        borderColor: AppColor.blueNormal,
                        onPressed: () {
                          Get.back();
                        },
                      ),
                    ],
                  ),
                ],
              ),
            ),
            SizedBox(),
          ],
        ),
      ),
    );
  }

  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();
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
