import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ongbut_ntmanh/const/colors.dart';
import '../bloc/login_bloc.dart';
import '../model/login_model.dart';

final TextEditingController _userController = TextEditingController();
final TextEditingController _passController = TextEditingController();
LoginModel? loginModels;
class LoginPage extends StatelessWidget {
  const LoginPage({super.key});
  @override
  Widget build(BuildContext context) {
    return
      BlocConsumer<LoginBloc, LoginState>(
      listener: (context, state) async {
        if(state is LoginSuccess){
          return showCustomDialog(context, 'Dang Nhap Thanh Cong!');
        }
        if(state is LoginFailed){
          return showCustomDialog(context,'Dang Nhap Khong Thanh Cong!');
        }
        if(_userController.text.isEmpty){
          return showCustomDialog(context,'Hay Nhap usename');
        }
        if(_passController.text.isEmpty){
          return showCustomDialog(context,'Hay Nhap pass');
        }
        // else{return showCustomDialog(context,'');}
        // if(state is LoginSuccess){
        //   _userController.text='';
        //   _passController.text='';
        // }
        // hàm sẽ đc gọi mỗi khi có sự thay đổi state của Bloc.ví dụ:
        // if(state is MyStateSuccess){
        //   // Thực hiện hành động
        // }
        // if(state is LoginSuccess){
        //   Navigator.push(
        //       context,
        //       MaterialPageRoute(
        //         builder: (_) => LoginDetailPage(),
        //
        //       ));
        // }
      },
      builder: (context, state) {
        return _buildUI(context);

      },
    );
  }
}

Widget _buildUI(BuildContext context){
  return
    Container(
      color: AppColors.background,
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: const [
                Text('Dang Ky & Tu Van',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 60, right: 60),
            child:
            TextFormField(
              decoration: const InputDecoration(
                labelText: 'Product Name',
              ),
              controller: _userController,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 60, right: 60),
            child:
            TextFormField(
              decoration: const InputDecoration(
                labelText: 'PassWord',
              ),
              controller: _passController,
            ),
          ),

          Padding(
            padding: const EdgeInsets.all(20.0),
            child: OutlinedButton(
                style: OutlinedButton.styleFrom(
                  minimumSize: const Size.fromHeight(50),
                  backgroundColor: AppColors.primary,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                ),
                onPressed: () async {
                  BlocProvider.of<LoginBloc>(context).add(
                      RequestLogin(
                          context: context,
                          username: _userController.text,
                          password: _passController.text));

                },
                child: Text("Dang Nhap".toUpperCase(),
                  style: const TextStyle(
                      color: AppColors.white,
                      fontWeight: FontWeight.bold),)
            ),
          ),
          BlocBuilder<LoginBloc, LoginState>(
              builder: (context, state) {
                if (state is LoginLoading) {
                  return const CircularProgressIndicator();
                }
                if (state is LoginFailed) {
                  return const Text('LoginFailed!');
                }
                if (state is LoginInitial) {
                  return
                    const Text('LoginInitial!');
                }
                if (state is LoginSuccess) {
                  return const Text('LoginSuccess!');
                }
                return const Text('');
              }),
        ],
      ),
    );
}

void showCustomDialog(BuildContext context, String mes) {
  showGeneralDialog(
    context: context,
    barrierLabel: "Barrier",
    barrierDismissible: true,
    barrierColor: Colors.black.withOpacity(0.5),
    transitionDuration: Duration(milliseconds: 700),
    pageBuilder: (_, __, ___) {
      return Center(
        child: Container(
          height: 240,
          margin: EdgeInsets.symmetric(horizontal: 20),
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40)),
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.fromLTRB(15, 60, 0, 15),
                child: Text(mes,style: const TextStyle(fontSize: 18,color: Colors.black),),
              ),
              Center(
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.primary,
                    onPrimary: Colors.black, // foreground
                  ),
                  onPressed: () => Navigator.pop(context, true),
                  child: const Text('OK', style: TextStyle(fontSize: 28)),),
              )
            ],
          ),
        ),
      );
    },
  );
}


