import 'package:didvan/main.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/alert_data.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:didvan/utils/action_sheet.dart';
import 'package:flutter/foundation.dart';
import 'package:home_widget/home_widget.dart';

class AuthenticationState extends CoreProvier {
  int _currentPageIndex = 0;
  String username = '';
  String password = '';
  String _verificationCode = '';
  bool isShowCustomDialog = true;
  bool hasPassword = true;

  set currentPageIndex(int value) {
    _currentPageIndex = value;
    notifyListeners();
  }

  int get currentPageIndex => _currentPageIndex;

  Future<void> confirmUsername() async {
  appState = AppState.isolatedBusy;
  final RequestService service = RequestService(
    RequestHelper.checkHasPassword,
    useAutherization: false,
    body: {'username': username},
  );
  await service.post();

  if (service.isSuccess) {
    appState = AppState.idle;

    final bool hasPassword = service.result['hasPassword'];
    this.hasPassword = hasPassword;

    if (hasPassword) {
      currentPageIndex = 1;
    } else {
      currentPageIndex = 2;
    }
  } else {
  appState = AppState.failed;
  String message = service.errorMessage;
  if (service.statusCode == 404) {
    message = 'کاربر موجود نمی‌باشد.';
  }
  ActionSheetUtils(navigatorKey.currentContext!)
      .showAlert(AlertData(message: message));
}
}

  Future<String?> login(UserProvider userProvider) async {
    appState = AppState.isolatedBusy;
    final RequestService service = RequestService(
      RequestHelper.login,
      useAutherization: false,
      body: {'username': username, "password": password},
    );
    await service.post();
    if (service.isSuccess && service.result['loggedIn']) {
      final token = service.result['token'];
      await userProvider.setAndGetToken(newToken: token);
      RequestService.token = token;
      if (!kIsWeb) {
        HomeWidget.saveWidgetData("token", token.toString());
      }
      await userProvider.getUserInfo();
      appState = AppState.idle;
      return token;
    } else {
      appState = AppState.failed;
      ActionSheetUtils(navigatorKey.currentContext!)
          .showAlert(AlertData(message: service.errorMessage));
    }
    return null;
  }

  Future<void> sendOtpToken() async {
    final service = RequestService(
      '${RequestHelper.otp}?username=$username',
      useAutherization: false,
    );
    await service.httpGet();
  }

  Future<bool> verifyOtpToken(String token) async {
    appState = AppState.isolatedBusy;
    final service = RequestService(
      RequestHelper.otp,
      body: {
        'code': token,
        'username': username,
      },
      useAutherization: false,
    );
    await service.post();
    appState = AppState.idle;
    if (service.isSuccess) {
      _verificationCode = token;
      currentPageIndex++;
      return true;
    }
    ActionSheetUtils(navigatorKey.currentContext!).showAlert(
      AlertData(
        message: service.isSuccess
            ? service.result['message']
            : 'کد وارد شده صحیح نمی‌باشد',
      ),
    );
    return false;
  }

  Future<bool> resetPassword(String password) async {
    appState = AppState.isolatedBusy;
    final service = RequestService(
      RequestHelper.changePassword,
      body: {
        'code': _verificationCode,
        'username': username,
        'password': password,
      },
      useAutherization: false,
    );
    await service.put();
    if (service.isSuccess) {
      ActionSheetUtils(navigatorKey.currentContext!).showAlert(
        AlertData(
          message: 'کلمه عبور با موفقیت تغییر کرد',
          aLertType: ALertType.success,
        ),
      );
      return true;
    }
    ActionSheetUtils(navigatorKey.currentContext!)
        .showAlert(AlertData(message: service.errorMessage));
    return false;
  }
}
