// Author: fengshengxiong
// Date: 2024/5/7
// Description: 状态视图

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tone_snap/data/enum/app_side_enum.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/global/app_config.dart';
import 'package:tone_snap/res/themes/app_colors.dart';

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

class ViewStateWidget extends StatelessWidget {
  const ViewStateWidget({
    super.key,
    required this.viewState,
    required this.child,
    this.cpiBgColor,
  });

  final ViewState viewState;
  final Widget child;
  final Color? cpiBgColor;

  @override
  Widget build(BuildContext context) {
    switch (viewState) {
      case ViewState.normal:
        return child;
      case ViewState.loading:
        return loadingView(backgroundColor: cpiBgColor);
      case ViewState.empty:
        return AppConfig.appSideEnum == AppSideEnum.sideA ? emptyViewA() : emptyViewB();
      case ViewState.error:
        return errorView();
    }
  }
}

/// 加载中视图
Widget loadingView({
  Color? color,
  Color? backgroundColor,
}) {
  return Center(
    child: CircularProgressIndicator(
      strokeWidth: 3,
      color: AppConfig.appSideEnum == AppSideEnum.sideA ? color : seedColor,
      backgroundColor: backgroundColor,
    ),
  );
}

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

/// 空视图2
Widget emptyViewB() {
  return Center(
    child: Image.asset(
      Assets.sideBEmpty,
      width: 180.w,
      height: 160.h,
    ),
  );
}

/// 错误视图
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,
      ),
    ),
  );
}
