import 'dart:async';

import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/dependency_injection.dart';
import 'package:mc_common_app/config/routes.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/user_models/user.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/user_view_model.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:sizer/sizer.dart';

import '../../theme/colors.dart';

class RegisterSelectionPage extends StatefulWidget {
  const RegisterSelectionPage({super.key});

  @override
  State<RegisterSelectionPage> createState() => _RegisterSelectionPageState();
}

class _RegisterSelectionPageState extends State<RegisterSelectionPage> {
  AppState appState = AppState();
  late UserVM userVM;

  @override
  void initState() {
    super.initState();
    appState = injector.get<AppState>();
    scheduleMicrotask(() {
      context.read<UserVM>().isAlreadyUserLoggedin(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(MyAssets.bnIntroPng),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          children: [
            Utils.mFlex(6),
            SvgPicture.asset(MyAssets.logo),
            Utils.mFlex(4),
            LocaleKeys.welcomeMessage.tr().toText(
                  fontSize: 22,
                  letterSpacing: -1,
                ),
            10.height,
            LocaleKeys.welcomeDes.tr().toText(
                  color: MyColors.lightTextColor,
                  fontSize: 16,
                  height: 23 / 24,
                ),
            Utils.mFlex(1),
            ShowFillButton(
              title: LocaleKeys.login.tr(),
              maxWidth: double.infinity,
              horizontalMargin: 20,
              onPressed: () {
                navigateWithName(context, AppRoutes.loginWithPassword);
              },
            ),
            20.height,
            ShowFillButton(
              title: LocaleKeys.signUp.tr(),
              maxWidth: double.infinity,
              isFlatButton: true,
              txtColor: Colors.black,
              onPressed: () {
                if (appState.currentAppType == AppType.customer) {
                  navigateWithName(context, AppRoutes.registerCustomer);
                } else {
                  navigateWithName(context, AppRoutes.registerProvider);
                }
              },
            ),

            20.height,

            Utils.mFlex(3),

            // 10.height,
            if (AppState().currentAppType == AppType.customer) ...[
              TextButton(
                onPressed: () {
                  appState.setIsViewOnly = true;
                  AppState().setUser = User(data: UserData(userInfo: GuestConsts().userInfo, accessToken: null, expiryDate: DateTime.now()));
                  navigateReplaceWithName(context, AppRoutes.dashboard);
                },
                child: LocaleKeys.continueAsGuest.tr().toText(color: MyColors.darkTextColor, fontSize: 18),
              ),
              10.height,
            ],

            TextButton(
              onPressed: () {},
              child: Text(
                "${AppState().currentAppType}",
                style: const TextStyle(
                  color: MyColors.darkPrimaryColor,
                  fontWeight: FontWeight.bold,
                  decoration: TextDecoration.underline,
                ),
              ),
            ),
            Utils.mFlex(3),
          ],
        ),
      ),
    );
  }
}
