
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_wisetronic/widgets/desktop/desktop_download_apps.dart';
import 'package:flutter_wisetronic/widgets/general/breadcrumbs.dart';
import 'package:flutter_wisetronic/widgets/general/double_back_to_close_app_wrapper.dart';
import 'package:flutter_wisetronic/widgets/mobile/mobile_download_apps.dart';
import 'package:responsive_builder/responsive_builder.dart';

import '../constants.dart';
import '../events/eventbus.dart';
import '../events/events.dart';
import '../generated/l10n.dart';
import '../store/actions.dart';
import '../store/store.dart';
import '../utils/http_util.dart';
import '../widgets/general/bottom_nav.dart';
import '../widgets/general/navigationbar.dart';
import '../widgets/mobile/MobileBottomNav.dart';
import '../widgets/mobile/mobile_navigation_drawer.dart';

class Download extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return DownloadState();
  }

}

class DownloadState extends State<Download> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  Map<String, dynamic> data;

  @override
  Widget build(BuildContext context) {
    store.dispatch(UpdateContext(context));
    if (data == null) {
      return Scaffold(
        body: Center(
          child: SpinKitWave(
            color: Colors.lightBlueAccent,
            size: 40.0,
          ),
        ),
      );
    }

    return ResponsiveBuilder(
      builder: (context, sizingInformation) =>
          Scaffold(
            key: _scaffoldKey,
            appBar: MiniNavigationBar(
              title: S.of(context).download,
              back: true,
              breadCrumbs: [BreadCrumb(S.of(context).downloads, null)],
              breadCrumbHeight: sizingInformation.deviceScreenType == DeviceScreenType.mobile ? null : Constants.BREADCRUMB_HEIGHT,
            ),
            drawer: sizingInformation.deviceScreenType == DeviceScreenType.mobile ? MobileNavigationDrawer() : null,
            body: ScreenTypeLayout(
              mobile: MobileDownloadApps(data),
              tablet: DesktopDownloadApps(data),
              desktop: Scrollbar(
                isAlwaysShown: true,
                child: DesktopDownloadApps(data),
              ),
            ),
            bottomNavigationBar: ScreenTypeLayout(
              mobile: MobileBottomNav(),
              tablet: BottomNav(),
              desktop: BottomNav(),
            ),
          ),
    );
  }

  @override
  void initState() {
    super.initState();
    eventBus.on<OpenDrawer>().listen((event) {
      if (mounted) {
        _scaffoldKey.currentState.openDrawer();
      }
    });
    _loadData();
  }

  void _loadData() {
    HttpUtil.httpGet('v1/get-wisetronic-download-page')
        .then((value) {
      print('$value');
      if (mounted) {
        setState(() {
          data = value;
        });
      }
    });
  }
}