// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:hoshan/data/model/ai/credit_model.dart';
import 'package:hoshan/ui/screens/splash/cubit/user_info_cubit.dart';
import 'package:hoshan/ui/theme/text.dart';

class CreditCost extends StatefulWidget {
  final Color? textColor;
  final Color? loadingColor;
  final bool call;
  const CreditCost(
      {super.key, this.textColor, this.call = true, this.loadingColor});

  @override
  State<CreditCost> createState() => _CreditCostState();
}

class _CreditCostState extends State<CreditCost> {
  @override
  void initState() {
    super.initState();

    // if (widget.call) {
    context.read<UserInfoCubit>().getUserInfo().then(
      (value) {
        context.read<UserInfoCubit>().changeCredit(CreditModel(
            credit: UserInfoCubit.userInfoModel.credit,
            freeCredit: UserInfoCubit.userInfoModel.freeCredit));
      },
    );
    // }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UserInfoCubit, UserInfoState>(
      builder: (context, state) {
        if (state is UserInfoInitial || state is UserInfoLoading) {
          return SpinKitThreeBounce(
            color:
                widget.loadingColor ?? Theme.of(context).colorScheme.onSurface,
            size: 18,
          );
        }
        return Text(
          ((UserInfoCubit.userInfoModel.credit ?? 0) +
                  (UserInfoCubit.userInfoModel.freeCredit ?? 0) +
                  (UserInfoCubit.userInfoModel.gift_credit ?? 0))
              .toString(),
          style: AppTextStyles.body4.copyWith(
              fontWeight: FontWeight.bold,
              color:
                  widget.textColor ?? Theme.of(context).colorScheme.onSurface),
        );
      },
    );
  }
}
