import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:image_picker/image_picker.dart';
import 'package:niogu_app/core/components/top_back_bar_app.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/providers/app_provider.dart';
import 'package:niogu_app/core/utils/extension_format.dart';
import 'package:niogu_app/core/utils/image_service.dart';
import 'package:niogu_app/core/utils/log_message.dart';
import 'package:niogu_app/core/widgets/custom_snackbar.dart';
import 'package:niogu_app/features/goods/products/domain/entities/product.dart';
import 'package:niogu_app/features/goods/products/presentation/providers/product_provider.dart';
import 'package:niogu_app/features/goods/products/presentation/widgets/product_variant_model.dart';
import 'package:niogu_app/features/goods/products/presentation/widgets/manage_varian_shimmer.dart';
import 'package:niogu_app/features/goods/products/presentation/widgets/variant_product_card.dart';
import 'package:sizer/sizer.dart';

class AddProductVariantScreen extends ConsumerStatefulWidget {
  final bool isMainOutlet;
  final bool isStockFixed;

  const AddProductVariantScreen({super.key, required this.isMainOutlet, required this.isStockFixed});

  @override
  ConsumerState<AddProductVariantScreen> createState() =>
      _AddProductVariantScreenState();
}

class _AddProductVariantScreenState
    extends ConsumerState<AddProductVariantScreen> {
  List<int> variants = [1, 2, 3];

  final List<GlobalKey<FormState>> _formKeys = [];

  final List<ProductVariantModel> _variantModels = [];

  final ScrollController _scrollController = ScrollController();

  final ImagePicker _picker = ImagePicker();

  final List<String?> _pathImageTemps = [];

  bool _isLoading = true;

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

    WidgetsBinding.instance.addPostFrameCallback((_) {
      Future.delayed(const Duration(milliseconds: 800), () {
        _fecthVariants();
      });
    });
  }

  @override
  void dispose() {
    for (var variant in _variantModels) {
      variant.dispose();
    }
    _scrollController.dispose();
    super.dispose();
  }

  void _fecthVariants() {
    final data = ref.read(productWithVariantProvider);
    if (data.isNotEmpty) {
      for (int i = 0; i < data.length; i++) {
        _formKeys.add(GlobalKey<FormState>());
        _variantModels.add(ProductVariantModel());
        _variantModels[i].imagePath = data[i].imagePath;
        _variantModels[i].nameController.text = data[i].name;
        _variantModels[i].stockController.text = data[i].stock
            .toStringWithoutTrailingZero();
        _variantModels[i].costPriceController.text = data[i].costPrice
            .toStringWithoutTrailingZero();
        _variantModels[i].sellingPriceController.text = data[i].sellingPrice
            .toStringWithoutTrailingZero();
      }
    } else {
      _formKeys.add(GlobalKey<FormState>());
      _variantModels.add(ProductVariantModel());
    }

    setState(() {
      _isLoading = false;
    });
  }

  void _addVariant() {
    setState(() {
      _formKeys.add(GlobalKey<FormState>());
      _variantModels.add(ProductVariantModel());
    });

    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (_scrollController.hasClients) {
        _scrollController.animateTo(
          _scrollController.position.maxScrollExtent,
          duration: const Duration(milliseconds: 400),
          curve: Curves.easeOut,
        );
      }
    });
  }

  Future<void> _removeVariant(int index) async {
    setState(() {
      _formKeys.removeAt(index);
      _variantModels[index].dispose();
      _variantModels.removeAt(index);
    });
  }

  Future<void> _showPickerOptions(int index) async {
    final bool isTablet = 100.w >= 600;
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(2.5.w)),
      ),
      constraints: const BoxConstraints(maxWidth: double.infinity),
      builder: (BuildContext context) {
        return SafeArea(
          child: Container(
            width: 100.w,
            padding: EdgeInsets.symmetric(vertical: 2.h, horizontal: 5.w),
            clipBehavior: Clip.hardEdge,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.vertical(top: Radius.circular(6.w)),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ListTile(
                  leading: Icon(Icons.photo_library, size: 6.w),
                  title: Text(
                    'Galeri',
                    style: TextStyle(
                      fontSize: isTablet
                          ? AppFontSize.medium.sp
                          : AppFontSize.small.sp,
                    ),
                  ),
                  onTap: () async {
                    await _getImage(ImageSource.gallery, index);
                    context.pop();
                  },
                ),

                SizedBox(height: 2.h),

                ListTile(
                  leading: Icon(Icons.photo_camera, size: 6.w),
                  title: Text(
                    'Kamera',
                    style: TextStyle(
                      fontSize: isTablet
                          ? AppFontSize.medium.sp
                          : AppFontSize.small.sp,
                    ),
                  ),
                  onTap: () async {
                    await _getImage(ImageSource.camera, index);
                    context.pop();
                  },
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Future<void> _getImage(ImageSource ImageSource, int index) async {
    try {
      final XFile? pickedFile = await _picker.pickImage(
        source: ImageSource,
        imageQuality: 70,
      );

      if (pickedFile != null) {
        File tempFile = File(pickedFile.path);

        String? savedPath = await ImageService.saveImageToLocalDirectory(
          tempFile,
          "product_variant_images",
        );

        if (savedPath != null) {
          _pathImageTemps.add(savedPath);
          ref.read(variantImageTempProvider.notifier).state = _pathImageTemps;
          setState(() {
            _variantModels[index].imagePath = savedPath;
          });
        }
      }
    } catch (e, st) {
      LogMessage.log.w(e.toString(), error: e, stackTrace: st);
      CustomSnackbar.showWarning(context, "Akses ditolak");
    }
  }

  Future<void> _removeImage(int index) async {
    setState(() {
      _variantModels[index].imagePath = null;
    });
  }

  Future<void> _cleanUpImages() async {
    for (var path in _pathImageTemps) {
      if (path != null) {
        await ImageService.deleteLocalImage(path);
      }
    }
  }

  void _saveVariant() {
    final lastKey = _formKeys.last;

    if (!lastKey.currentState!.validate()) {
      return;
    }

    final List<ProductWithVariant> variants = [];

    try {
      for (var _variantModel in _variantModels) {
        final String? imagePath = _variantModel.imagePath;

        final String name = _variantModel.nameController.text.trim();
        
        final double stock = widget.isStockFixed
            ? double.parse(_variantModel.stockController.text.trim())
            : 0.0;
        
        final double costPrice = double.parse(
          _variantModel.costPriceController.text.trim(),
        );
        
        final double sellingPrice = double.parse(
          _variantModel.sellingPriceController.text.trim(),
        );

        if (costPrice >= sellingPrice) {
          CustomSnackbar.showError(
            context,
            "Harga jual harus lebih besar dari modal",
          );
          setState(() {});
          return;
        }

        final productWithVariant = ProductWithVariant(
          imagePath: imagePath,
          name: name,
          stock: stock,
          costPrice: costPrice,
          sellingPrice: sellingPrice,
        );

        variants.add(productWithVariant);
      }
    } catch (e) {
      CustomSnackbar.showError(context, "Format angka tidak valid");
      return;
    }

    ref.read(productWithVariantProvider.notifier).state = variants;

    CustomSnackbar.showSuccess(
      context,
      "${variants.length} varian produk berhasil dibuat",
    );

    context.pop();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final isTablet = 100.w >= 600;

        const backgroundColor = Color(0xFFF9FAFB);

        final currentUserRole = ref.watch(currentUserRoleProvider);

        return PopScope(
          canPop: false,
          onPopInvokedWithResult: (didPop, result) async {
            if (didPop) return;

            await _cleanUpImages();

            context.pop();
          },
          child: SafeArea(
            top: false,
            bottom: true,
            right: false,
            left: false,
            child:                 Scaffold(
                  resizeToAvoidBottomInset: false,
                  backgroundColor: backgroundColor,
                  appBar: TopBackBarApp(
                    title: "Atur Varian Produk",
                    onTap: () async {
                      await _cleanUpImages();

                      context.pop();
                    },
                  ),
                  body: _isLoading
                      ? const ManageVariantShimmer()
                      : SingleChildScrollView(
                          controller: _scrollController,
                          padding: EdgeInsets.fromLTRB(5.w, 3.h, 5.w, 12.h),
                          child: Column(
                            children: [
                              Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 2.w,
                                  vertical: 1.h,
                                ),
                                margin: EdgeInsets.only(bottom: 2.h),
                                decoration: BoxDecoration(
                                  color: Colors.blue[50],
                                  borderRadius: BorderRadius.circular(4.w),
                                ),
                                child: Row(
                                  children: [
                                    Icon(
                                      Icons.info_outline,
                                      color: AppColor.primaryColor,
                                      size: 5.w,
                                    ),

                                    SizedBox(width: 3.w),

                                    Expanded(
                                      child: Text(
                                        "Setiap varian dapat memiliki harga dan stok yang berbeda.",
                                        style: TextStyle(
                                          color: AppColor.primaryColor,
                                          fontSize: isTablet
                                              ? AppFontSize.medium.sp
                                              : AppFontSize.small.sp,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),

                              ..._variantModels.asMap().entries.map((entry) {
                                final index = entry.key;
                                final variant = _variantModels[index];

                                return Padding(
                                  padding: EdgeInsets.only(bottom: 3.h),
                                  child: VariantProductCard(
                                    userRole: currentUserRole!,
                                    isMainOutlet: widget.isMainOutlet,
                                    isEditing: false,
                                    formKey: _formKeys[index],
                                    length: _variantModels.length,
                                    variant: variant,
                                    index: index,
                                    isStockFixed: widget.isStockFixed,
                                    nameController: variant.nameController,
                                    stockController: variant.stockController,
                                    costPriceController:
                                        variant.costPriceController,
                                    sellingPriceController:
                                        variant.sellingPriceController,
                                    onTapRemoveVarian: () async =>
                                        await _removeVariant(index),
                                    onTapVariantImageAdd: () async =>
                                        await _showPickerOptions(index),
                                    onTapVariantImageRemove: () async =>
                                        await _removeImage(index),
                                  ),
                                );
                              }),

                              GestureDetector(
                                onTap: () async {
                                  FocusScope.of(context).unfocus();

                                  await Future.delayed(
                                    const Duration(milliseconds: 400),
                                  );

                                  final lastKey = _formKeys.last;

                                  if (!lastKey.currentState!.validate()) return;

                                  for (var variant in _variantModels) {
                                    final double costPrice = double.parse(
                                      variant.costPriceController.text.trim(),
                                    );
                                    final double sellingPrice = double.parse(
                                      variant.sellingPriceController.text
                                          .trim(),
                                    );

                                    if (costPrice >= sellingPrice) {
                                      CustomSnackbar.showError(
                                        context,
                                        "Harga jual harus lebih besar dari modal",
                                      );
                                      return;
                                    }
                                  }

                                  _addVariant();
                                },
                                child: Container(
                                  width: double.infinity,
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 4.w,
                                    vertical: 2.h,
                                  ),
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(3.w),
                                  ),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Icon(
                                        Icons.add_circle_outline_rounded,
                                        color: AppColor.primaryColor,
                                      ),

                                      SizedBox(width: 3.w),

                                      Text(
                                        "Tambah Varian Baru",
                                        style: TextStyle(
                                          color: AppColor.primaryColor,
                                          fontSize: AppFontSize.medium.sp,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),

                  bottomNavigationBar: _isLoading
                      ? const SizedBox()
                      : Container(
                          padding: EdgeInsets.all(5.w),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black.withOpacity(0.05),
                                blurRadius: 10,
                                offset: const Offset(0, -5),
                              ),
                            ],
                          ),
                          child: SizedBox(
                            height: 6.5.h,
                            child: ElevatedButton(
                              onPressed: _saveVariant,
                              style: ElevatedButton.styleFrom(
                                backgroundColor: AppColor.primaryColor,
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(2.5.w),
                                ),
                                elevation: 0,
                              ),
                              child: Text(
                                "Simpan Perubahan",
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: AppFontSize.medium.sp,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                        ),
                ),
          ),
        );
      },
    );
  }
}
