
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_wisetronic/widgets/desktop/desktop_plain_page.dart';
import 'package:flutter_wisetronic/widgets/mobile/mobile_plain_page.dart';
import 'package:responsive_builder/responsive_builder.dart';

import '../models/blog.dart';
import '../store/actions.dart';
import '../store/store.dart';
import '../utils/http_util.dart';
import '../utils/utils.dart';

class PlainPage extends StatefulWidget {
  final int businessId;
  final String slug;

  const PlainPage(this.slug, {this.businessId, Key? key}) :
      super(key: key);

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

class PlainPageState extends State<PlainPage> {
  Blog blog;

  @override
  Widget build(BuildContext context) {

    store.dispatch(UpdateContext(context));

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

    return ResponsiveBuilder(
      builder: (context, sizingInformation) =>
          ScreenTypeLayout(
            mobile: MobilePlainPage(blog),
            tablet: DesktopPlainPage(blog),
            desktop: DesktopPlainPage(blog),
          ),
    );
  }

  @override
  void initState() {
    super.initState();
    loadData();
  }

  void loadData() {
    HttpUtil.httpGet(
      'v1/special-page',
      businessId: widget.businessId,
      queryParameters: {
        'bid': widget.slug,
      }
    ).then((value) {
      blog = Blog.fromJson(value);
      setState(() {});
    }).onError((error, stackTrace) {
      Utils.showMessageDialog(context, error);
    });
  }
}