import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/views/widgets/state_handlers/empty_connection.dart';
import 'package:flutter/material.dart';

class StateHandler<T extends CoreProvier> extends StatelessWidget {
  final T state;
  final Widget Function(BuildContext context, T state) builder;
  final VoidCallback onRetry;
  final bool enableEmptyState;
  final Widget? placeholder;
  final Widget? emptyState;
  final double topPadding;
  const StateHandler({
    Key? key,
    required this.builder,
    required this.onRetry,
    required this.state,
    this.emptyState,
    this.enableEmptyState = false,
    this.topPadding = 0,
    this.placeholder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (enableEmptyState && state.appState == AppState.idle) {
      return Container(
        padding: EdgeInsets.only(top: topPadding),
        alignment: Alignment.center,
        child: SingleChildScrollView(
          child: emptyState ?? const Text('Custom Widget Required!'),
        ),
      );
    }
    switch (state.appState) {
      case AppState.idle:
        return builder(context, state);
      case AppState.busy:
        return Padding(
          padding: EdgeInsets.only(top: topPadding),
          child: Center(
            child: placeholder ??
                Image.asset(
                  Assets.loadingAnimation,
                  width: 100,
                  height: 100,
                ),
          ),
        );
      case AppState.failed:
        return Center(child: EmptyConnection(onRetry: onRetry));
      default:
        return Container();
    }
  }
}
