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

/// 四种视图状态
enum ViewStateEnum { normal, error, loading, empty }

class ViewState extends StatelessWidget {
  const ViewState({
    super.key,
    required this.viewStateEnum,
    required this.child,
  });

  final ViewStateEnum viewStateEnum;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    switch (viewStateEnum) {
      case ViewStateEnum.normal:
        return child;
      case ViewStateEnum.loading:
        return loadingView();
      case ViewStateEnum.empty:
        return emptyView();
      case ViewStateEnum.error:
        return errorView();
    }
  }
}

/// 加载中视图
Widget loadingView({
  Color? color,
  Animation<Color?>? valueColor,
  Color? backgroundColor,
  double? value,
}) {
  return Center(
    child: CircularProgressIndicator(
      color: const Color(0xff4ECA8C),
      valueColor: valueColor,
      backgroundColor: backgroundColor ?? Colors.white,
      value: value,
    ),
  );
}

/// 空视图
Widget emptyView({String? msg, Color? textColor}) {
  return Center(
    child: Text(
      msg ?? 'No data',
      textAlign: TextAlign.center,
      style: TextStyle(
        color: textColor ?? Colors.white,
        fontSize: 15.sp,
      ),
    ),
  );
}

/// 错误视图
Widget errorView({String? msg, Color? textColor}) {
  return Center(
    child: Text(
      msg ?? 'An error occurred, please try again later',
      textAlign: TextAlign.center,
      style: TextStyle(
        color: textColor ?? Colors.white,
        fontSize: 15.sp,
      ),
    ),
  );
}
