import 'package:farm_tpf/authentication/bloc/authentication_bloc.dart';
import 'package:farm_tpf/data/repository/user_repository.dart';
import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
import 'package:farm_tpf/presentation/custom_widgets/button_icon_widget.dart';
import 'package:farm_tpf/presentation/screens/profile/sc_change_password.dart';
import 'package:farm_tpf/presentation/screens/profile/sc_update_profile.dart';
import 'package:farm_tpf/utils/const_icons.dart';
import 'package:farm_tpf/utils/pref.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info/package_info.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class AccountScreen extends StatefulWidget {
  @override
  _AccountScreenState createState() => _AccountScreenState();
}

class _AccountScreenState extends State<AccountScreen> {
  var pref = LocalPref();
  UserRepository _userRepository = UserRepository();
  PackageInfo _packageInfo = PackageInfo(
    version: '1.0.0',
    buildNumber: '1.',
    appName: '',
    packageName: '',
  );

  Future<void> _initPackageInfo() async {
    final PackageInfo info = await PackageInfo.fromPlatform();
    setState(() {
      _packageInfo = info;
    });
  }

  @override
  void initState() {
    super.initState();
    _initPackageInfo();
  }

  _clickSignOut() async {
    BlocProvider.of<AuthenticationBloc>(context).add(AuthenticationLogoutRequested());
    // context.bloc<AuthenticationBloc>().add(AuthenticationLogoutRequested());
    try {
      var pushKey = await pref.getString(DATA_CONST.PUSH_KEY);
      if (pushKey.isNotEmpty) {
        _userRepository.deleteFcmToken(pushKey).then((value) {}).catchError((err) {}).whenComplete(() {
          pref.saveString(DATA_CONST.TOKEN_KEY, "");
          pref.saveString(DATA_CONST.PUSH_KEY, "");
          pref.saveString(DATA_CONST.CURRENT_FULL_NAME, "");
        });
      }
    } catch (e) {
      pref.saveString(DATA_CONST.CURRENT_FULL_NAME, "");
      pref.saveString(DATA_CONST.TOKEN_KEY, "");
      pref.saveString(DATA_CONST.PUSH_KEY, "");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            const SizedBox(
              height: 8,
            ),
            Container(
                padding: const EdgeInsets.all(8),
                color: Colors.white,
                child: const Text(
                  'Cá nhân',
                  style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
                )),
            Expanded(
                child: Column(children: [
              ButtonIconWidget(
                  title: 'Tài khoản',
                  leadingIcon: AppIcons.icUser,
                  trailingIcon: AppIcons.icArrowRight,
                  isTopBorder: false,
                  onTap: () {
                    Get.to(UpdateProfileScreen());
                  }),
              ButtonIconWidget(
                  title: 'Bảo mật',
                  leadingIcon: AppIcons.icSecurity,
                  trailingIcon: AppIcons.icArrowRight,
                  isTopBorder: false,
                  isBottomBorder: false,
                  onTap: () {
                    Get.to(ChangePasswordScreen());
                  }),
              Container(
                width: double.infinity,
                height: 20,
                color: Colors.grey[200],
              ),
              Container(
                padding: const EdgeInsets.only(left: 12, right: 8, top: 14, bottom: 14),
                child: Row(
                  children: [
                    const Expanded(
                        child: Text(
                      'Phiên bản',
                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.w100),
                    )),
                    Text("${_packageInfo.version}.${_packageInfo.buildNumber}")
                  ],
                ),
              ),
              ButtonIconWidget(
                  title: 'Đăng xuất',
                  titleStyle: const TextStyle(fontSize: 16, fontWeight: FontWeight.w100, color: Colors.red),
                  leadingIcon: AppIcons.icLogout,
                  trailingIcon: AppIcons.icArrowRight,
                  onTap: () {
                    _clickSignOut();
                  }),
            ])),
          ],
        ),
      ),
    );
  }
}
