import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:hub/features/storage/index.dart';

import 'package:hub/shared/mixins/switcher_mixin.dart';

import '/components/templates_components/welcome_template_component/welcome_template_component_widget.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import 'welcome_page_model.dart';

export 'welcome_page_model.dart';

class WelcomePage extends StatefulWidget with Switcher {
  const WelcomePage({super.key});

  @override
  State<WelcomePage> createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  late WelcomePageModel _model;

  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => WelcomePageModel());

    // On page load action.
    SchedulerBinding.instance.addPostFrameCallback((_) async {
      if (isAndroid == true) {
        await StorageHelper().set(SecureStorageKey.deviceType.value, 'Android');
        setState(() {});
      } else if (isiOS == true) {
        await StorageHelper().set(SecureStorageKey.deviceType.value, 'iOS');
        setState(() {});
      } else {
        await StorageHelper().set(SecureStorageKey.deviceType.value, 'Web');
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    _model.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    StorageHelper().context = context;
    return GestureDetector(
      onTap: () => _model.unfocusNode.canRequestFocus
          ? FocusScope.of(context).requestFocus(_model.unfocusNode)
          : FocusScope.of(context).unfocus(),
      child: Scaffold(
        key: scaffoldKey,
        backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
        body: SafeArea(
          top: true,
          child: wrapWithModel(
            model: _model.welcomeTemplateComponentModel,
            updateCallback: () => setState(() {}),
            updateOnChange: true,
            child: WelcomeTemplateComponentWidget(
              toggleSignUpPage: () async {
                await Switcher.toggleSignUpPage(context);
              },
              toggleSignInPage: () async {
                await Switcher.toggleSignInPage(context);
              },
            ),
          ),
        ),
      ),
    );
  }
}
