import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../app.dart';

class ForgotPasswordPage extends StatefulWidget {
  final Map<String, dynamic> params;
  ForgotPasswordPage({
    Key? key,
    required this.params,
  }) : super(key: key);

  @override
  State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
}

class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
  final formKey = GlobalKey<FormState>();
  // final VisibilityController vc = Get.put(VisibilityController());
  TextEditingController passwordCtrl = new TextEditingController();
  bool isLoading = false;

  Future<void> _updatePassword() async {
    KeyboardUtil.hideKeyboard(context);
    if (!formKey.currentState!.validate()) return;

    setState(() => isLoading = true);

    final resetRes = await AuthApi.resetPassword({
      'phone': widget.params['phone'],
    });

    if (resetRes) {
      final Map<String, dynamic> params = {
        'phone': widget.params['phone'],
        'token': widget.params['token'], // auth sms code
        'password': passwordCtrl.text,
      };

      final result = await AuthApi.updatePassword(params);
      if (result) {
        showSnack('success'.tr, 'password_updated'.tr);
        Get.close(3);
      } else
        showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);
    } else
      showSnack('error'.tr, 'error_occurred'.tr, SnackType.ERROR);

    setState(() => isLoading = false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          splashColor: Theme.of(context).splashColor,
          splashRadius: 20.h,
          onPressed: () {
            Get.close(3);
          },
          icon: Icon(
            Icons.arrow_back,
            color: ThemeColor.black,
            size: 26.h,
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(left: 30.w, right: 30.w, top: 30.h),
          child: Form(
            key: formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'reset_password'.tr,
                  style: new TextStyle(
                    color: Color(0xFF3A3A3A),
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w700,
                  ),
                ),
                AppTheme.appSizeDivider20H,
                AppTheme.appColorDivider,
                AppTheme.appSizeDivider20H,
                Text('password'.tr, style: AppTheme.styleColorC4C4),
                SizedBox(height: 15.h),
                GetBuilder<VisibilityController>(
                  init: VisibilityController(),
                  builder: (vc) => MyTextFormField(
                    controller: passwordCtrl,
                    suffix: PasswordVisibilityWidget(),
                    obscureText: vc.obscureText.value,
                    isPassword: true,
                    label: 'password'.tr,
                  ),
                ),
                AppTheme.appSizeDivider20H,
                AppTheme.appSizeDivider20H,
                isLoading
                    ? CustomLoader()
                    : ColoredButton(
                        title: 'update_password'.tr,
                        callback: _updatePassword,
                        btnColor: ThemeColor.mainColor,
                      ),
                AppTheme.appSizeDivider20H,
              ],
            ),
          ),
        ),
      ),
    );
  }
}
