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

import '../../../common/config.dart';
import '../../../services/index.dart';
import '../store_detail/store_detail_screen.dart';

class StoreDetailScreenFromConfig extends StatefulWidget {
  final storeId;

  const StoreDetailScreenFromConfig({super.key, this.storeId});

  @override
  State<StoreDetailScreenFromConfig> createState() =>
      _StoreDetailScreenFromConfigState();
}

class _StoreDetailScreenFromConfigState
    extends State<StoreDetailScreenFromConfig> {
  final Services _services = Services();
  Store? store;
  bool isLoading = true;
  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.endOfFrame.then((_) async {
      store = await _services.api.getStoreInfo(widget.storeId);
      if (mounted) {
        if (store == null) {
          Navigator.pop(context);
          return;
        }
        setState(() {
          isLoading = false;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return Center(child: kLoadingWidget(context));
    }
    return StoreDetailScreen(store: store);
  }
}
