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_page.dart';
import 'package:e_commerce_training/src/page/setting_account/setting_account_cubit.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/custom_appbar.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

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

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

class _SettingAccountPageState extends State<SettingAccountPage> {
  late SettingAccountCubit _cubit;
  RefreshController _refreshController = RefreshController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    AppRepository repository = AppRepository();
    _cubit = SettingAccountCubit(repository);
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: Scaffold(
        appBar: appBar(
          context,
          R.string.change_password.tr(),
        ),
        body: BlocProvider(
          create: (BuildContext context) => _cubit,
          child: BlocListener<SettingAccountCubit, SettingAccountState>(
            listener: (context, state) {
              if (state is SettingAccountFailure)
                Util.showErrorSnackBar(
                  context,
                  state.error,
                );
              if (!(state is SettingAccountLoading)) _refreshController.refreshCompleted();
            },
            child: BlocBuilder<SettingAccountCubit, SettingAccountState>(
              builder: (context, state) {
                return Scaffold(
                  backgroundColor: R.color.grey200,
                  body: ListView(
                    children: [
                      SizedBox(
                        height: 10,
                      ),
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                          child: ChangePassPage(),
                        ),
                      SizedBox(
                        height: 30,
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(horizontal: 15),
                        height: 180.h,
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: R.color.white,
                          borderRadius: BorderRadius.circular(5),
                          border: Border.all(
                            width: 0.5,
                            color: R.color.black.withOpacity(0.5),
                          ),
                        ),
                        child: Center(
                          child: Text(
                            R.string.sign_out.toUpperCase().tr(),
                            style: TextStyle(
                              fontSize: 16.sp,
                              fontWeight: FontWeight.bold,
                              color: R.color.primaryColor,
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
