
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_wisetronic/utils/http_util.dart';
import 'package:flutter_wisetronic/utils/utils.dart';
import 'package:flutter_wisetronic/widgets/desktop/desktop_buy_service.dart';
import 'package:flutter_wisetronic/widgets/desktop/desktop_renew_minioffice.dart';
import 'package:flutter_wisetronic/widgets/mobile/mobile_buy_service.dart';
import 'package:flutter_wisetronic/widgets/mobile/mobile_renew_minioffice.dart';
import 'package:responsive_builder/responsive_builder.dart';

import '../constants.dart';
import '../models/business.dart';
import '../store/actions.dart';
import '../store/store.dart';
import '../widgets/desktop/desktop_renew_license.dart';
import '../widgets/mobile/mobile_renew_license.dart';

class BuyService extends StatefulWidget {
  final int gid;
  final String serviceName;
  final String domain;
  final int sid;

  const BuyService(this.gid, this.serviceName, {Key? key, this.domain, this.sid = 0}) :
      super(key: key);

  @override
  State<StatefulWidget> createState() => BuyServiceState();
}

class BuyServiceState extends State<BuyService> {
  Map<String, dynamic> data;

  @override
  Widget build(BuildContext context) {

    store.dispatch(UpdateContext(context));

    if (data == null ) {
      return new Scaffold(
        body: Center(
          child: SpinKitWave(
            color: Colors.lightBlueAccent,
            size: 40.0,
          ),
        ),
      );
    }

    return ResponsiveBuilder(
      builder: (context, sizingInformation) =>
          ScreenTypeLayout(
            mobile: MobileBuyService(data),
            tablet: DesktopBuyService(data),
            desktop: DesktopBuyService(data),
          ),
    );
  }

  @override
  void initState() {
    super.initState();
    _loadData();
  }
  
  void _loadData() {
    HttpUtil.httpGet(
      'v1/get-buy-service-info/${widget.serviceName}/${widget.gid}',
      businessId: Constants.BUSINESS_ID,
      queryParameters: {
        'store_id': '${widget.sid}',
      }
    ).then((value) {
      data = value;
      data['domain'] = widget.domain;
      print('data: $data');
      setState(() {});
    }).onError((error, stackTrace) {
      Utils.showMessageDialog(context, error);
    });
  }
}