import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/loader.dart';
import 'package:taafee_mobile/common/widgets/text.dart';
import 'package:rx_future/rx_future.dart';

import '../const/const.dart';

// ignore: must_be_immutable
class RxViewer extends StatelessWidget {
  final RxFuture rxFuture;
  final double? width;
  final double? height;
  final double? loaderWidth;
  final double? loaderHeight;
  final double? errorWidth;
  final double? errorHeight;
  final bool? withSizedBox;
  final Color? color;
  final Function() child;
  final Widget? errorWidget;
  final Widget? customLoader;
  bool _isLoading = false;
  final bool? withPagination;
  RxViewer(
      {super.key,
      required this.rxFuture,
      this.color,
      this.height,
      this.width,
      this.loaderHeight,
      this.loaderWidth,
      this.errorHeight,
      this.errorWidth,
      this.withSizedBox,
      this.errorWidget,
      this.customLoader,
      this.withPagination,
      required this.child});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (withPagination != null) {
        if (withPagination!) {
          _isLoading = rxFuture.loading && rxFuture.result.isFirstPage;
        } else {
          _isLoading = rxFuture.loading;
        }
      } else {
        _isLoading = rxFuture.loading;
      }
      if (_isLoading) {
        return (customLoader != null)
            ? customLoader!
            : (withSizedBox ?? false)
                ? SizedBox(
                    width: loaderWidth,
                    height: loaderHeight,
                    child: Loader(
                      width: width,
                      height: height,
                      color: color,
                    ).center(),
                  )
                : SizedBox(
                    width: width,
                    height: height,
                    child: Loader(
                      width: width,
                      height: height,
                      color: color,
                    ).center(),
                  );
      }
      if (rxFuture.hasError) {
        return errorWidget ??
            SizedBox(
              width: errorWidth,
              height: errorHeight,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Lottie.asset(
                    'assets/animations/Wifi.json',
                    repeat: false,
                    width: 120,
                  ).paddingAll(5),
                  RegularTextWidget(
                    'you_have_no_internet_connection'.tr,
                    textAlign: TextAlign.center,
                    color: AppColors.textColor,
                    fontSize: 18.0,
                  )
                ],
              ).center(),
            );
      } else {
        return child();
      }
    });
  }
}
