import 'package:flutter/material.dart';
import 'package:tour_travel_agr/helper/components_widget/password_input.dart';
import 'package:tour_travel_agr/helper/components_widget/widget_button.dart';
import 'package:tour_travel_agr/helper/widget_responsive.dart';
import 'package:tour_travel_agr/helper/components_widget/widget_text_field.dart';
import 'package:tour_travel_agr/resource/assets.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/routes.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/style.dart';

class LoginView extends StatelessWidget {
  LoginView({super.key});
  final TextEditingController whatsappController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorManager.backgroundColor,
      body: ScreenResponsive(
        widget: BodyWidget(
          whatsappController: whatsappController,
          passwordController: passwordController,
        ),
        widthScreen: MediaQuery.of(context).size.width,
      ),
    );
  }
}

class BodyWidget extends StatefulWidget {
  const BodyWidget({
    Key? key,
    required this.whatsappController,
    required this.passwordController,
  }) : super(key: key);

  final TextEditingController whatsappController;
  final TextEditingController passwordController;

  @override
  State<BodyWidget> createState() => _BodyWidgetState();
}

class _BodyWidgetState extends State<BodyWidget> {
  bool buttonLoginActive = false;
  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          padding: EdgeInsets.only(
            left: AppPadding.p20,
            right: AppPadding.p20,
            top: AppPadding.safeAreaTop(context),
            bottom: AppPadding.safeAreaBot(context),
          ),
          child: Column(
            children: [
              Center(
                child: Container(
                  padding: const EdgeInsets.only(
                    top: 40,
                  ),
                  child: Image(
                    width: 197,
                    height: 73,
                    image: AssetImage(
                      Assets.logoGreen,
                    ),
                  ),
                ),
              ),
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      const SizedBox(
                        height: 148,
                      ),
                      InputTextField(
                        controller: widget.whatsappController,
                        labelText: "No. Whatsapp",
                        hintText: "628...",
                        marginActive: false,
                        onChanged: (val) {
                          if (widget.whatsappController.text.length >= 7 &&
                              widget.passwordController.text.isNotEmpty) {
                            setState(() {
                              buttonLoginActive = true;
                            });
                          } else {
                            setState(() {
                              buttonLoginActive = false;
                            });
                          }
                        },
                      ),
                      SizedBox(
                        height: AppMargin.m20,
                      ),
                      PasswordInput(
                        passwordController: widget.passwordController,
                        onChanged: (val) {
                          if (widget.whatsappController.text.length >= 7 &&
                              widget.passwordController.text.isNotEmpty) {
                            setState(() {
                              buttonLoginActive = true;
                            });
                          } else {
                            setState(() {
                              buttonLoginActive = false;
                            });
                          }
                        },
                      ),
                      Container(
                        margin: EdgeInsets.only(
                          top: AppMargin.m20,
                          bottom: AppMargin.m25,
                        ),
                        child: Row(
                          children: [
                            const Spacer(),
                            GestureDetector(
                              onTap: () {
                                Navigator.pushNamed(
                                  context,
                                  Routes.forgotPasswordRoute,
                                );
                              },
                              child: Text(
                                "Lupa kata sandi",
                                style: getRegularStyle(
                                  color: ColorManager.primary,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      CustomButton(
                        text: "Login",
                        colorButton: buttonLoginActive
                            ? ColorManager.primary
                            : Colors.grey,
                        onTap: () {
                          if (buttonLoginActive) {
                            // TODO:dosomething here
                          }
                        },
                      ),
                      Container(
                        margin: const EdgeInsets.only(
                          top: 28,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              "Belum mempunyai akun ? ",
                              style: getRegularStyle(
                                color: Colors.black,
                              ),
                            ),
                            GestureDetector(
                              onTap: () {
                                Navigator.pushNamed(
                                  context,
                                  Routes.registerRoute,
                                );
                              },
                              child: Text(
                                "Daftar",
                                style: getSemiBoldStyle(
                                  color: ColorManager.link,
                                ),
                              ),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
        Positioned(
          top: 0,
          left: 0,
          child: Image(
            height: 150,
            image: AssetImage(
              Assets.elipse,
            ),
          ),
        ),
      ],
    );
  }
}
