import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:loader_overlay/loader_overlay.dart';

import '../../../app.dart';
import '../controller.dart';
import 'wishlist_item_card.dart';

class WishlistsListWidget extends StatelessWidget {
  const WishlistsListWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetX<WishlistController>(
      init: WishlistController(),
      builder: (wc) {
        final bool isLastPage = wc.wlState.lastPage.value;
        return LoaderOverlay(
          useDefaultLoading: false,
          overlayColor: Colors.transparent,
          overlayWidget: CustomLoader(),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 10.h, top: 20.h),
                child: Text(
                  'wishlist'.tr,
                  style: AppTheme.subHeaderTxtStyle,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 30.w, right: 30.w),
                child: AppTheme.appColorDivider,
              ),
              Expanded(
                child: wc.obx(
                  (state) => state != null
                      ? RefreshIndicator(
                          onRefresh: wc.refreshList,
                          color: ThemeColor.mainColor,
                          child: ListView.builder(
                            padding: EdgeInsets.only(top: 12, left: 30.w, right: 30.w),
                            shrinkWrap: true,
                            controller: wc.scroll,
                            itemCount: state.length + 1,
                            itemBuilder: (context, index) {
                              if (index < state.length) {
                                return WishlistCartItem(model: state[index]);
                              } else if (index == state.length && !isLastPage)
                                return Padding(
                                  padding: const EdgeInsets.only(top: 10, bottom: 40),
                                  child: Center(child: CustomLoader()),
                                );
                              else {
                                return SizedBox.shrink();
                              }
                            },
                          ),
                        )
                      : retryBuilder(wc.fetchProducts),
                  onLoading: CustomLoader(),
                  onEmpty: ItemNotFoundWidget(),
                  onError: (error) => retryBuilder(wc.fetchProducts),
                ),
              ),
              /*  Expanded(
              child: PagewiseListView(
                padding: EdgeInsets.only(left: 30.w, right: 30.w),
                pageLoadController: wc.state.pagewiseLoadController,
                shrinkWrap: true,
                itemBuilder: (context, entry, index) => WishlistCartItem(model: entry as WishlistModel),
                loadingBuilder: (_) => CenterWidget(
                  child: CustomLoader(),
                ),
                retryBuilder: (_, callback) => CenterWidget(
                  child: retryBuilder(callback),
                ),
                noItemsFoundBuilder: (_) => ItemNotFoundWidget(),
              ),
            ), */
            ],
          ),
        );
      },
    );
  }
}

class CenterWidget extends StatelessWidget {
  final Widget child;
  CenterWidget({required this.child});
  @override
  Widget build(BuildContext context) {
    return Container(
      color: ThemeColor.white,
      height: 1.sh,
      child: Stack(
        children: [
          Positioned(
            top: 0.3.sh,
            left: 0,
            right: 0,
            child: child,
          ),
        ],
      ),
    );
  }
}
