import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/widgets/Loader/gif_loader_container.dart';
import 'package:diplomaticquarterapp/widgets/errors/app_embedded_error.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NetworkBaseView extends StatefulWidget {
  final BaseViewModel baseViewModel;
  final Widget child;
  final bool isLocalLoader;
  final bool isLoading;

  NetworkBaseView({Key key, this.baseViewModel, this.child, this.isLocalLoader = false, this.isLoading});

  @override
  _NetworkBaseViewState createState() => _NetworkBaseViewState();
}

class _NetworkBaseViewState extends State<NetworkBaseView> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: (widget.baseViewModel != null)
          ? buildBaseViewWidget()
          : Center(
              child: widget.isLoading
                  ? CircularProgressIndicator(
                      backgroundColor: Colors.white,
                      valueColor: AlwaysStoppedAnimation<Color>(
                        Colors.grey[500],
                      ),
                    )
                  : widget.child,
            ),
    );
  }

  buildBaseViewWidget() {
    switch (widget.baseViewModel.state) {
      case ViewState.ErrorLocal:
      case ViewState.Idle:
      case ViewState.BusyLocal:
        return widget.child;
        break;
      case ViewState.Busy:
        if (widget.isLocalLoader)
          return Container(
            height: 100,
            child: Center(
              child: CircularProgressIndicator(
                backgroundColor: Colors.white,
                valueColor: AlwaysStoppedAnimation<Color>(
                  Colors.grey[500],
                ),
              ),
            ),
          );
        else
          return Container(
            height: MediaQuery.of(context).size.height,
            child: Stack(
              children: [
                Container(
                  color: Colors.grey.withOpacity(0.6),
                ),
                Container(child: GifLoaderContainer(), margin: EdgeInsets.only(bottom: MediaQuery.of(context).size.height * 0.09))
              ],
            ),
          );
        break;
      case ViewState.Error:
        return Center(
          child: AppEmbeddedError(
            error: widget.baseViewModel.error,
          ),
        );
        break;
    }
  }
}
