import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/screens/home/state.dart';
import 'package:qadirneyriz/setting/setting.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/utils/tools/tools.dart';
import 'package:qadirneyriz/widgets/custom_appbar.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:qadirneyriz/widgets/custom_button.dart';
import 'package:qadirneyriz/widgets/custom_textfield.dart';

class ChangePassScreen extends StatefulWidget {
  const ChangePassScreen({super.key});

  @override
  State<ChangePassScreen> createState() => _ChangePassScreenState();
}

class _ChangePassScreenState extends State<ChangePassScreen> {
  TextEditingController nameController = TextEditingController();
  TextEditingController passController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    final phone = setting.userLocalDb.getUser().mobile ?? '';
    final name = setting.userLocalDb.getUser().name ?? '';
    return Consumer<HomeState>(
      builder: (context, value, child) {
        return CustomScrollView(
          slivers: <Widget>[
            CustomAppbar(
              title: AppLocalizations.of(context)!.profile,
            ),
            SliverToBoxAdapter(
              child: Container(
                decoration: BoxDecoration(
                    color: config.ui.secendGreen,
                    boxShadow: [
                      BoxShadow(
                          color: config.ui.mainGray.withOpacity(.2),
                          spreadRadius: 10,
                          offset: const Offset(0, 5),
                          blurRadius: 8)
                    ],
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(50),
                        bottomRight: Radius.circular(50))),
                child: Padding(
                  padding: const EdgeInsets.all(30.0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        decoration: BoxDecoration(
                            color: Colors.white.withOpacity(.2),
                            shape: BoxShape.circle),
                        child: Padding(
                          padding: const EdgeInsets.all(20.0),
                          child: Icon(
                            Icons.person,
                            size: 50,
                            color: Colors.white,
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Text(
                        name,
                        style: TextStyle(
                            fontSize: 16,
                            color: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 2,
                      ),
                      Text(
                        phone,
                        style: TextStyle(
                            fontSize: 16,
                            color: Colors.white,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            SliverToBoxAdapter(
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 70),
                child: Column(
                  children: [
                    CustomTextField(
                        label: AppLocalizations.of(context)!.changename,
                        hintText: name,
                        textEditingController: nameController,
                        textInputType: TextInputType.visiblePassword),
                    CustomTextField(
                        label: AppLocalizations.of(context)!.changepass,
                        hintText: AppLocalizations.of(context)!.newpass,
                        isPass: true,
                        textEditingController: passController,
                        textInputType: TextInputType.visiblePassword),
                    SizedBox(
                      height: 60,
                    ),
                    chnagePassMethod(value)
                  ],
                ),
              ),
            )
          ],
        );
      },
    );
  }

  Widget chnagePassMethod(HomeState state) {
    switch (state.statusEditProfile) {
      case Status.loading:
        return CustomButton(
            hieght: 50, text: AppLocalizations.of(context)!.loading);

      default:
        return CustomButton(
          hieght: 50,
          text: AppLocalizations.of(context)!.submit,
          onPressed: () async {
            if (nameController.text == '' && passController.text == '') {
              Tools.showCustomSnackBar(
                text: AppLocalizations.of(context)!.doyouredit,
                isError: true,
                context,
              );
            } else {
              final status = await state.editProfile(
                  name: nameController.text, pass: passController.text);

              if (status == Status.ready) {
                Tools.showCustomSnackBar(
                  text: AppLocalizations.of(context)!.editdone,
                  isError: false,
                  context,
                );
                setState(() {});
              } else {
                Tools.showCustomSnackBar(
                  text: state.errorsEditProfile == null
                      ? state.messageEditProfile ??
                          AppLocalizations.of(context)!.haserror
                      : Tools.combineErrorMessages(
                          state.errorsEditProfile ?? {}),
                  isError: true,
                  context,
                );
              }
            }
          },
        );
    }
  }
}
