import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';

import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';

class ResetPwdPage extends StatefulWidget {
  @override
  _ResetPwdPageState createState() => _ResetPwdPageState();
}

class _ResetPwdPageState extends State<ResetPwdPage> {
  ///显示密码
  bool showPass = false;
  bool showPass3 = false;
  bool showPass2 = false;

  ///账户控制器
  TextEditingController confirmController = TextEditingController();

  ///密码控制器
  TextEditingController passController = TextEditingController();

  ///旧密码
  TextEditingController oldPassController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          "sc_images/person/sc_icon_edit_userinfo_bg.png",
          width: ScreenUtil().screenWidth,
          height: ScreenUtil().screenHeight,
          fit: BoxFit.fill,
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          resizeToAvoidBottomInset: false,
          appBar: SocialChatStandardAppBar(
            title: SCAppLocalizations.of(context)!.resetLoginPassword,
            actions: [],
          ),
          body: SafeArea(top: false,child: Column(
            children: [
              Container(
                width: ScreenUtil().screenWidth,
                padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 8.w),
                child: text(
                  SCAppLocalizations.of(context)!.resetLoginPasswordtTips1,
                  maxLines: 2,
                  textColor: Colors.white,
                  fontSize: 10.sp,
                ),
              ),
              SizedBox(height: 10.w),
              Row(
                children: [
                  SizedBox(width: 15.w),
                  text(
                    SCAppLocalizations.of(context)!.account,
                    fontSize: 15.sp,
                    textColor: Colors.white,
                  ),
                ],
              ),
              Container(
                width: ScreenUtil().screenWidth,
                padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.w),
                margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                  color: Color(0xff18F2B1).withOpacity(0.1)
                ),
                child: text(
                  AccountStorage().getCurrentUser()?.userProfile?.account ?? "",
                  textColor: Colors.white54,
                  fontSize: 14.sp,
                ),
              ),
              SizedBox(height: 10.w),
              Row(
                children: [
                  SizedBox(width: 15.w),
                  text(
                    SCAppLocalizations.of(context)!.inputYourOldPassword,
                    fontSize: 15.sp,
                    textColor: Colors.white,
                  ),
                ],
              ),
              Container(
                width: ScreenUtil().screenWidth,
                padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.w),
                margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                    color: Color(0xff18F2B1).withOpacity(0.1)
                ),
                child: TextField(
                  controller: oldPassController,
                  maxLength: 30,
                  maxLines: 1,
                  inputFormatters: [
                    FilteringTextInputFormatter.deny(RegExp(r'\s'))
                  ],

                  obscureText: !showPass3,
                  textInputAction: TextInputAction.done,
                  onChanged: (s) {
                    setState(() {});
                  },
                  decoration: InputDecoration(
                    hintText:
                    SCAppLocalizations.of(context)!.enterYourOldPassword,
                    hintStyle: TextStyle(
                      color: Colors.white54,
                      fontSize: 14.sp,
                    ),
                    counterText: '',
                    contentPadding: EdgeInsets.only(top: 0.w),
                    isDense: true,
                    filled: false,
                    focusColor: Colors.transparent,
                    hoverColor: Colors.transparent,
                    border: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    focusedErrorBorder: InputBorder.none,
                    suffix: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        setState(() {
                          showPass3 = !showPass3;
                        });
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 8),
                        child: Image.asset(
                          !showPass3
                              ? "sc_images/login/sc_icon_pass.png"
                              : "sc_images/login/sc_icon_pass1.png",
                          gaplessPlayback: true,
                          color: Colors.white54,
                          width: 15.w,
                        ),
                      ),
                    ),
                  ),
                  style: TextStyle(fontSize: 12.sp, color: Colors.white),
                ),
              ),
              SizedBox(height: 10.w),
              Row(
                children: [
                  SizedBox(width: 15.w),
                  text(
                    SCAppLocalizations.of(context)!.setYourPassword,
                    fontSize: 15.sp,
                    textColor: Colors.white,
                  ),
                ],
              ),
              Container(
                width: ScreenUtil().screenWidth,
                padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.w),
                margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                    color: Color(0xff18F2B1).withOpacity(0.1)
                ),
                child: TextField(
                  controller: passController,
                  maxLength: 30,
                  maxLines: 1,
                  inputFormatters: [
                    FilteringTextInputFormatter.deny(RegExp(r'\s'))
                  ],

                  obscureText: !showPass,
                  textInputAction: TextInputAction.done,
                  onChanged: (s) {
                    setState(() {});
                  },
                  decoration: InputDecoration(
                    hintText:
                    SCAppLocalizations.of(context)!.enterYourNewPassword,
                    hintStyle: TextStyle(
                      color: Colors.white54,
                      fontSize: 14.sp,
                    ),
                    counterText: '',
                    contentPadding: EdgeInsets.only(top: 0.w),
                    isDense: true,
                    filled: false,
                    focusColor: Colors.transparent,
                    hoverColor: Colors.transparent,
                    border: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    focusedErrorBorder: InputBorder.none,
                    suffix: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        setState(() {
                          showPass = !showPass;
                        });
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 8),
                        child: Image.asset(
                          !showPass
                              ? "sc_images/login/sc_icon_pass.png"
                              : "sc_images/login/sc_icon_pass1.png",
                          gaplessPlayback: true,
                          color: Colors.white54,
                          width: 15.w,
                        ),
                      ),
                    ),
                  ),
                  style: TextStyle(fontSize: 12.sp, color: Colors.white),
                ),
              ),
              Container(
                width: ScreenUtil().screenWidth,
                padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.w),
                margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                  color: Colors.black12,
                ),
                child: TextField(
                  controller: confirmController,
                  maxLength: 30,
                  maxLines: 1,
                  inputFormatters: [
                    FilteringTextInputFormatter.deny(RegExp(r'\s'))
                  ],

                  obscureText: !showPass2,
                  textInputAction: TextInputAction.done,
                  onChanged: (s) {
                    setState(() {});
                  },
                  decoration: InputDecoration(
                    hintText: SCAppLocalizations.of(context)!.confirmYourPassword,
                    hintStyle: TextStyle(
                      color: Colors.white54,
                      fontSize: 14.sp,
                    ),
                    counterText: '',
                    contentPadding: EdgeInsets.only(top: 0.w),
                    isDense: true,
                    filled: false,
                    focusColor: Colors.transparent,
                    hoverColor: Colors.transparent,
                    border: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    focusedErrorBorder: InputBorder.none,
                    suffix: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        setState(() {
                          showPass2 = !showPass2;
                        });
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 8),
                        child: Image.asset(
                          !showPass2
                              ? "sc_images/login/sc_icon_pass.png"
                              : "sc_images/login/sc_icon_pass1.png",
                          gaplessPlayback: true,
                          color: Colors.black54,
                          width: 15.w,
                        ),
                      ),
                    ),
                  ),
                  style: TextStyle(fontSize: 12.sp, color: Colors.white),
                ),
              ),
              Row(
                children: [
                  SizedBox(width: 10.w),
                  Expanded(
                    child: text(
                      SCAppLocalizations.of(context)!.resetLoginPasswordtTips2,
                      fontSize: 10.sp,
                      textColor: Colors.white,
                      maxLines: 3,
                    ),
                  ),
                  SizedBox(width: 10.w),
                ],
              ),
              SizedBox(height: 20.w),
              GestureDetector(
                child: Container(
                  height: 44.w,
                  width: 260.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(8),
                    color: SocialChatTheme.primaryLight,
                  ),
                  child: text(
                    SCAppLocalizations.of(context)!.submit,
                    fontSize: 16.sp,
                    textColor: Colors.white,
                  ),
                ),
                onTap: () {
                  resetPwd();
                },
              ),
            ],
          ),),
        ),
      ],
    );
  }

  void resetPwd() {
    if (confirmController.text.isNotEmpty &&
        passController.text.isNotEmpty &&
        oldPassController.text.isNotEmpty) {
      if (confirmController.text == passController.text) {
        SCLoadingManager.show(context: context);
        SCAccountRepository()
            .updatePwd(passController.text, oldPassController.text)
            .then((d) {
              SCLoadingManager.hide();
              SCTts.show(SCAppLocalizations.of(context)!.operationSuccessful);
              SCNavigatorUtils.goBack(context);
            })
            .catchError((e) {
              SCLoadingManager.hide();
            });
      } else {
        SCTts.show(SCAppLocalizations.of(context)!.theTwoPasswordsDoNotMatch);
      }
    }
  }
}
