import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/parking/parking_slot.dart';
import 'package:provider/provider.dart';

import '../../features/qr_parking/qr_parking_view_model.dart';
import '../../theme/colors.dart';
import '../../widgets/appbar/app_bar_widget.dart';
import '../../widgets/buttons/custom_button.dart';
import '../../widgets/routes/custom_page_route.dart';



class ParkingPage extends StatefulWidget {
  const ParkingPage({super.key});

  @override
  State<ParkingPage> createState() => _ParkingPageState();
}

class _ParkingPageState extends State<ParkingPage> {


  Future<void> _readQR(BuildContext context) async {
    final vm = context.read<QrParkingViewModel>();
    final model = await vm.scanAndGetParking();
    if (model == null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(vm.error ?? "Invalid Qr Code")),
      );
      return;
    }

    Navigator.of(context).push(
      CustomPageRoute(
        page: ChangeNotifierProvider.value(
          value: vm,
          child: ParkingSlot(model: model),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      final vm = context.read<QrParkingViewModel>();
      await vm.getIsSaveParking();
      if (!mounted) return;

      if (vm.isSavePark && vm.qrParkingModel != null) {
        Navigator.of(context).push(
          CustomPageRoute(
            page: ChangeNotifierProvider.value(
              value: vm,
              child: ParkingSlot(model: vm.qrParkingModel!),
            ),
          ),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final vm = context.watch<QrParkingViewModel>();
    return Scaffold(
      backgroundColor: AppColors.scaffoldBgColor,
      appBar: CustomAppBar(
        onBackPressed: () => Navigator.of(context).pop(),
        onLanguageChanged: (_) {},
        hideLogoAndLang: true,
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              padding: EdgeInsets.symmetric(horizontal: 24.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    LocaleKeys.parking.tr(context: context),
                    style: TextStyle(
                      color: AppColors.textColor,
                      fontSize: 27.f,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Container(
                    decoration: RoundedRectangleBorder()
                        .toSmoothCornerDecoration(
                      color: AppColors.whiteColor,
                      borderRadius: 24.r,
                      hasShadow: true,
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(16.h), child: LocaleKeys.parkingDescription.tr(context: context).toText12(isBold: true, color: AppColors.textColor)),
                  ).paddingOnly(top: 16, bottom: 16),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(24.r),
                    child: Transform.flip(
                      flipX: getIt.get<AppState>().isArabic(),
                      child: Utils.buildImgWithAssets(
                        icon: AppAssets.carParkingService,
                        fit: BoxFit.fitHeight,
                        height: 480.h,
                        width: 520.w,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),

          Container(
            decoration: RoundedRectangleBorder()
                .toSmoothCornerDecoration(
              color: AppColors.whiteColor,
              borderRadius: 24.r,
              hasShadow: true,
            ),
            child: Padding(
              padding: EdgeInsets.all(24.h),
              child: CustomButton(
                text: LocaleKeys.scanQRCode.tr(context: context),
                onPressed: () => _readQR(context),
                // always non-null
                isDisabled: vm.isLoading,
                backgroundColor: AppColors.primaryRedColor,
                borderColor: AppColors.primaryRedColor,
                fontSize: 18.f,
                height: 56.h,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }
}


