import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mohem_flutter_app/api/login_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/check_mobile_app_version_model.dart';
import 'package:mohem_flutter_app/models/member_login_list_model.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/input_widget.dart';

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

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

class _LoginScreenState extends State<LoginScreen> {
  TextEditingController username = TextEditingController();
  TextEditingController password = TextEditingController();

  CheckMobileAppVersionModel? _checkMobileAppVersion;
  MemberLoginListModel? _memberLoginList;

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

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

  void performLogin() async {
    Utils.showLoading(context);
    try {
      _checkMobileAppVersion = await LoginApiClient().checkMobileAppVersion();
      _memberLoginList = await LoginApiClient().memberLogin(username.text, password.text);
      AppState().setMemberLoginListModel = _memberLoginList;
      AppState().setUserName = username.text;
      print(_memberLoginList?.toJson());
      Utils.hideLoading(context);
      Navigator.pushNamed(context, AppRoutes.verifyLogin);
    } catch (ex) {
      print(ex);
      Utils.handleException(ex, null);
      Utils.hideLoading(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    username.text="15153";
    password.text="s12s12s12";
    return Scaffold(
      body: Column(
        children: [
          const SizedBox(height: 23),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(21.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(child: SizedBox()),
                      Row(
                        children: [
                          LocaleKeys.english.tr().toText14(color: MyColors.textMixColor).onPress(() {
                            context.setLocale(const Locale("en", "US"));
                          }),
                          Container(
                            width: 1,
                            color: MyColors.darkWhiteColor,
                            height: 16,
                            margin: const EdgeInsets.only(left: 10, right: 10),
                          ),
                          LocaleKeys.arabic.tr().toText14().onPress(() {
                            context.setLocale(const Locale("ar", "SA"));
                          }),
                        ],
                      ),
                    ],
                  ),
                  Expanded(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        LocaleKeys.login.tr().toText24(isBold: true),
                        LocaleKeys.pleaseEnterLoginDetails.tr().toText16(),
                        16.height,
                        InputWidget(LocaleKeys.username.tr(), "123456", username),
                        12.height,
                        InputWidget(LocaleKeys.password.tr(), "xxxxxx", password, isObscureText: true),
                        9.height,
                        Align(
                          alignment: Alignment.centerRight,
                          child: LocaleKeys.forgotPassword.tr().toText12(isUnderLine: true, color: MyColors.textMixColor).onPress(() {
                            Navigator.pushNamed(context, AppRoutes.forgotPassword);
                          }),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
          DefaultButton(LocaleKeys.login.tr(), () async {
            //     context.setLocale(const Locale("en", "US")); // to change Loacle

            SystemChannels.textInput.invokeMethod('TextInput.hide');
            performLogin();
          }).insideContainer
        ],
      ),
    );
  }
}
