import 'package:diplomaticquarterapp/core/service/packages_offers/PackagesOffersServices.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';

class ShimmerLoadingWidget extends StatefulWidget {
  Future<void> Function() onPreProccess;
  Widget Function() loadingWidget;
  Widget Function() realWidget;
  Function() onFinish;
  ShimmerLoadingWidget({this.onPreProccess, this.loadingWidget, this.realWidget, this.onFinish});

  void loadWidget() {}

  @override
  State<StatefulWidget> createState() => _ShimmerLoadingWidgetState();
}

class _ShimmerLoadingWidgetState extends State<ShimmerLoadingWidget> {
  var service = locator<OffersAndPackagesServices>();

  @override
  Widget build(BuildContext context) {
    Stream<bool> preProccess = (() async* {
      await super.widget.onPreProccess();
    })();

    // TODO: implement build
    return StreamBuilder(
        stream: preProccess,
        builder: (context, snapshot) {
          Widget widget;
          if (snapshot.hasError) {
            widget = Texts(
              "Error happened",
              color: Colors.red,
              fontSize: 20,
              bold: true,
            );
          } else {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                break;
              case ConnectionState.active:
                break;
              case ConnectionState.waiting:
                widget = Shimmer.fromColors(child: super.widget.loadingWidget(), baseColor: Colors.red, highlightColor: Colors.yellow);
                break;
              case ConnectionState.done:
                widget = super.widget.realWidget();
                if (super.widget.onFinish != null) super.widget.onFinish();
                break;
            }
          }

          return widget;
        });
  }
}
