import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tangheem/api/user_api_client.dart';
import 'package:tangheem/app_state/app_state.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/consts.dart';
import 'package:tangheem/classes/utils.dart';
import 'package:tangheem/extensions/int_extensions.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.dart';
import 'package:tangheem/ui/dialogs/change_password_dialog.dart';
import 'package:tangheem/ui/dialogs/general_dialog.dart';
import 'package:tangheem/ui/screens/change_password_screen.dart';

class UserProfileScreen extends StatefulWidget {
  static const String routeName = "/profile";

  UserProfileScreen({Key key}) : super(key: key);

  @override
  _UserProfileScreenState createState() {
    return _UserProfileScreenState();
  }
}

class _UserProfileScreenState extends State<UserProfileScreen> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void deleteUserAccount() async {
    Utils.showLoading(context);
    try {
      await UserApiClient().deleteAccount(AppState().userId);
      SharedPreferences prefs = await SharedPreferences.getInstance();
      await prefs.remove(GlobalConsts.userAuthData);
      AppState().setAuthenticationModel(null);
      Utils.showToast("تم حذف الحساب");
      Navigator.pop(context);
      Utils.hideLoading(context);
    } catch (ex) {
      if (mounted) {
        Utils.hideLoading(context);
        Utils.handleException(ex, null);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    bool isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;

    List<Widget> _actionsList = [
      button("تغيير كلمة المرور", isPortrait).onPress(() {
        Navigator.pushNamed(context, ChangePasswordScreen.routeName);
      }),
      Container(width: 50, height: 1, color: ColorConsts.dark99Text).paddingOnly(top: 15, bottom: 15),
      button("تسجيل الخروج", isPortrait).onPress(() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        await prefs.remove(GlobalConsts.userAuthData);
        AppState().setAuthenticationModel(null);
        Utils.showToast("تسجيل خروج المستخدم");
        Navigator.pop(context);
      }),
      Container(width: 50, height: 1, color: ColorConsts.dark99Text).paddingOnly(top: 15, bottom: 15),
      button("حذف الحساب", isPortrait).onPress(() {
        showDialog(
          context: context,
          barrierColor: Colors.white.withOpacity(0.2),
          builder: (BuildContext context) => GeneralDialog(
            message: "هل تود حذف حسابك عبر تطبيق تنغيم؟",
            backgroundColor: Color(0xff598A8D),
            buttonTitle: "تأكيد الحذف",
            buttonBorderColor: ColorConsts.brownLightColor,
            onTap: () {
              Navigator.pop(context);
              deleteUserAccount();
            },
          ),
        );
      }),
    ];
    return SizedBox(
      width: double.infinity,
      child: Stack(
        alignment: Alignment.topCenter,
        children: [
          Image.asset(
            "assets/icons/new/home_dark.jpg",
            fit: BoxFit.cover,
            height: isPortrait ? double.infinity : null,
            width: isPortrait ? null : double.infinity,
            opacity: const AlwaysStoppedAnimation(1),
          ),
          Container(color: ColorConsts.darkText.withOpacity(0.8)),
          isPortrait
              ? Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    100.height,
                    Image.asset('assets/icons/new/Tangeem-logo-W.png', width: 50),
                    100.height,
                    Container(
                      width: 78,
                      height: 78,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: ColorConsts.dark2Text,
                      ),
                      child: SvgPicture.asset(
                        "assets/icons/new/user_acount.svg",
                        color: Colors.white,
                      ),
                    ),
                    14.height,
                    (AppState().userName ?? "").toText(25),
                    50.height,
                    ..._actionsList
                  ],
                )
              : Row(
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset('assets/icons/new/Tangeem-logo-W.png', width: 75),
                        30.height,
                        Container(
                          width: 78,
                          height: 78,
                          decoration: BoxDecoration(shape: BoxShape.circle, color: ColorConsts.dark2Text),
                          child: SvgPicture.asset(
                            "assets/icons/new/user_acount.svg",
                            color: Colors.white,
                          ),
                        ),
                        24.height,
                        (AppState().userName ?? "").toText(25),
                      ],
                    ).expanded,
                    Column(mainAxisAlignment: MainAxisAlignment.center, children: _actionsList).expanded,
                  ],
                ).paddingOnly(right: 36, left: 36),
        ],
      ),
    );
  }

  Widget button(String title, bool isPortrait) {
    return Container(
      height: 36,
      width: isPortrait ? 165 : 200,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(32),
        border: Border.all(color: Colors.white, width: 1),
      ),
      child: title.toText(13),
    );
  }
}
