import 'dart:async';
import 'dart:convert';

import 'package:car_provider_app/classes/colors.dart';
import 'package:car_provider_app/config/routes.dart';
import 'package:car_provider_app/extensions/int_extensions.dart';
import 'package:car_provider_app/extensions/string_extensions.dart';
import 'package:car_provider_app/generated/locale_keys.g.dart';
import 'package:car_provider_app/utils/navigator.dart';
import 'package:car_provider_app/utils/utils.dart';
import 'package:car_provider_app/extensions/widget_extensions.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../../api/client/user_api_client.dart';
import '../../api/shared_prefrence.dart';
import '../../classes/app_state.dart';
import '../../classes/utils.dart';
import '../../models/user/refresh_token.dart';
import '../../models/user/user.dart';

class SelectionPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            mFlex(5),
            SvgPicture.asset("assets/images/logo.svg"),
            "Logo".toText(fontSize: 45, isBold: true),
            mFlex(3),
            LocaleKeys.firstTimeLogIn.tr().toText(fontSize: 18, isBold: true).onPress(
              () {
                navigateWithName(context, AppRoutes.registerSelection);
              },
            ),
            mFlex(1),
            LocaleKeys.alreadySigned.tr().toText(fontSize: 18, isBold: true).onPress(
              () async {
                String token = await SharedPrefManager.getUserToken();
                String refreshToken = await SharedPrefManager.getRefreshToken();
                if (token.isNotEmpty) {
                  Utils.showLoading(context);
                  RefreshToken refresh = await UserApiClent().RefreshTokenAPI(token, refreshToken);
                  Utils.hideLoading(context);
                  if (refresh.messageStatus == 1) {
                    SharedPrefManager.setUserToken(refresh.data!.accessToken ?? "");
                    SharedPrefManager.setRefreshToken(refresh.data!.refreshToken ?? "");
                    String mdata = await SharedPrefManager.getData();
                    print(mdata);
                    UserInfo info = UserInfo.fromJson(jsonDecode(mdata));
                    User user = new User();
                    user.data = new UserData(accessToken: refresh.data!.accessToken ?? "", refreshToken: refresh.data!.refreshToken ?? "", userInfo: info);
                    AppState().setUser = user;
                    print(AppState().getUser.data?.userInfo?.roleName);
                    navigateWithName(context, AppRoutes.dashboard);
                  } else {
                    String accessToken = await SharedPrefManager.getUserToken();
                    String refreshToken = await SharedPrefManager.getRefreshToken();
                    String mdata = await SharedPrefManager.getData();
                    UserInfo info = UserInfo.fromJson(jsonDecode(mdata));
                    User user = new User();
                    user.data = new UserData(accessToken: accessToken, refreshToken: refreshToken, userInfo: info);
                    AppState().setUser = user;
                    print(AppState().getUser.data?.userInfo?.roleName);
                    navigateWithName(context, AppRoutes.dashboard);
                  }
                } else {
                  Utils.showToast(LocaleKeys.login_once.tr());
                }
              },
            ),
            35.height,
            TextButton(
              style: TextButton.styleFrom(
                backgroundColor: Colors.white,
              ),
              onPressed: () {
                if (EasyLocalization.of(context)?.currentLocale?.countryCode == "SA")
                  context.setLocale(const Locale("en", "US"));
                else
                  context.setLocale(const Locale('ar', 'SA'));
              },
              child: Text(
                LocaleKeys.english.tr(),
              ),
            ),
            mFlex(5),
          ],
        ),
      ),
    );
  }
}
