import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/page/change_pass/change_pass_cubit.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/text_field_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ChangePassPage extends StatefulWidget {
  const ChangePassPage({Key? key}) : super(key: key);

  @override
  _ChangePassPageState createState() => _ChangePassPageState();
}

class _ChangePassPageState extends State<ChangePassPage> {
  late ChangePassCubit _cubit;
  TextEditingController _currentPasswordController = TextEditingController();
  TextEditingController _newPasswordController = TextEditingController();
  TextEditingController _rePasswordController = TextEditingController();
  FocusNode? _currentPasswordFocus, _newPasswordFocus, _rePasswordFocus;
  String? _errorCurrentPass, _errorNewPass, _errorRePass;
  RefreshController _refreshController = RefreshController();

  @override
  void initState() {
    // TODO: implement initState
    _newPasswordController = TextEditingController();
    _rePasswordController = TextEditingController();
    _currentPasswordFocus = FocusNode();
    _newPasswordFocus = FocusNode();
    _rePasswordFocus = FocusNode();
    AppRepository repository = AppRepository();
    _cubit = ChangePassCubit(repository);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.grey200,
      appBar: AppBar(
        backgroundColor: R.color.primaryColor,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
                child: Center(
                    child: Text(
                      R.string.change_password.tr(),
                      style: TextStyle(fontSize: 20.sp),
                    ))),
            InkWell(
              child: Text(
                R.string.save.tr(),
                style: TextStyle(fontSize: 16.sp),
              ),
              onTap: () {},
            ),
          ],
        ),
      ),
      body: BlocProvider(
        create: (BuildContext context) => _cubit,
        child: BlocConsumer<ChangePassCubit, ChangePassState>(
          listener: (context, state) {
            if (state is ChangePassFailure) Util.showErrorSnackBar(context, state.error);
            if (!(state is ChangePassLoading)) _refreshController.refreshCompleted();
          },
          builder: (BuildContext context,ChangePassState state) {
            if (state is ValidateCurrentPasswordError) _errorCurrentPass = state.error!;
            if (state is ValidateRePasswordError) _errorRePass = state.error;
            return GestureDetector(
              onTap: ()=>Util.hideKeyboard(context),
              child: SingleChildScrollView(
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                  child: Column(
                    children: [
                      buildInputCurrentPassword(context),
                      SizedBox(
                        height: 15,
                      ),
                      buildInputNewPassword(context),
                      SizedBox(
                        height: 15,
                      ),
                      buildInputRePassword(context),
                      SizedBox(
                        height: 15,
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget buildInputCurrentPassword(BuildContext context) {
    return TextFieldWidget(
      controller: _currentPasswordController,
      textInputAction: TextInputAction.next,
      keyboardType: TextInputType.visiblePassword,
      hintText: R.string.curr_password.tr(),
      labelText: R.string.curr_password.tr(),
      isPassword: true,
      icon: R.drawable.ic_password,
      errorText: _errorCurrentPass,
      onChanged: (text) => _cubit.validateCurrentPassword,
      focusNode: _currentPasswordFocus,
      onSubmitted: (text) {
        Util.navigateNextFocusChange(context, _currentPasswordFocus!, _newPasswordFocus!);
        Util.hideKeyboard(context);
      },
    );
  }

  Widget buildInputNewPassword(BuildContext context) {
    return TextFieldWidget(
      controller: _newPasswordController,
      textInputAction: TextInputAction.next,
      keyboardType: TextInputType.visiblePassword,
      hintText: R.string.new_password.tr(),
      labelText: R.string.new_password.tr(),
      isPassword: true,
      icon: R.drawable.ic_password,
      errorText: _errorNewPass,
      onChanged: (text) => _cubit.validateNewPassword,
      focusNode: _newPasswordFocus,
      onSubmitted: (text) {
        Util.navigateNextFocusChange(context, _newPasswordFocus!, _rePasswordFocus!);
        Util.hideKeyboard(context);
      },
    );
  }

  Widget buildInputRePassword(BuildContext context) {
    return TextFieldWidget(
      controller: _rePasswordController,
      textInputAction: TextInputAction.go,
      keyboardType: TextInputType.visiblePassword,
      hintText: R.string.confirm_new_password.tr(),
      labelText: R.string.confirm_password.tr(),
      isPassword: true,
      icon: R.drawable.ic_password,
      errorText: _errorRePass,
      onChanged: (text) => _cubit.validateRePassword,
      focusNode: _rePasswordFocus,
      onSubmitted: (text) =>
          _cubit.changePass(
            _currentPasswordController.text,
            _newPasswordController.text,
            _rePasswordController.text,
          ),
    );
  }
}
