import 'package:cims_apps/application/component/button/back_button_view.dart';
import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/core/utils/size_config.dart';
import 'package:cims_apps/features/auth/login/view/password_view.dart';
import 'package:cims_apps/features/auth/login/view/phone_number_view.dart';
import 'package:cims_apps/features/auth/login/view_model/login_view_model.dart';
import 'package:cims_apps/features/bottom_navigation_view.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

  @override
  State<LoginView> createState() => _LoginViewState();
}

class _LoginViewState extends State<LoginView> {
  int currentPage = 0;
  PageController pageController = PageController(initialPage: 0);
  TextEditingController phoneNumberController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  @override
  void dispose() {
    // TODO: implement dispose
    pageController.dispose();
    phoneNumberController.dispose();
    passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => LoginViewModel(),
      child: WillPopScope(
        onWillPop: () async {
          if (currentPage == 1) {
            currentPage--;
            pageController.jumpToPage(0);
            return false;
          }
          return true;
        },
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            toolbarHeight: 70,
            backgroundColor: Colors.white,
            surfaceTintColor: Colors.white,
            automaticallyImplyLeading: false,
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                BackButtonView(),
                const Text('Sign In'),
                SizedBox(
                  width: SizeConfig.width * 0.1,
                )
              ],
            ),
            shape: const RoundedRectangleBorder(
                side: BorderSide(color: ColorPalette.slate200)),
          ),
          body: PageView(
            physics: NeverScrollableScrollPhysics(),
            controller: pageController,
            children: [
              PhoneNumberView(
                  nextStep: nextStep, controller: phoneNumberController),
              PasswordView(nextStep: nextStep, controller: passwordController)
            ],
          ),
        ),
      ),
    );
  }

  void nextStep() {
    if (pageController.page == 0) {
      currentPage++;
      pageController.jumpToPage(1);
    } else {
      routePush(
        context,
        page: const BottomNavigationView(),
        routeType: RouteType.pushReplace,
      );
    }
  }
}
