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:provider/provider.dart';

class ChangePasswordState extends CoreProvier {
  int _currentPageIndex = 0;
  String _verificationCode = '';
  String newPassword = '';
  String? username;

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

  int get currentPageIndex => _currentPageIndex;

  Future<void> sendOtp() async {
    appState = AppState.isolatedBusy;

    if (username == null) {
      final userProvider = navigatorKey.currentContext?.read<UserProvider>();
      username = userProvider?.user.username ?? userProvider?.user.phoneNumber;
    }

    if (username == null) {
      appState = AppState.failed;
      ActionSheetUtils(navigatorKey.currentContext!)
          .showAlert(AlertData(message: 'خطا در دریافت اطلاعات کاربر'));
      return;
    }

    final service = RequestService(
      '${RequestHelper.otp}?username=$username',
      useAutherization: true,
    );
    await service.httpGet();

    if (service.isSuccess) {
      appState = AppState.idle;
    } else {
      appState = AppState.failed;
      ActionSheetUtils(navigatorKey.currentContext!)
          .showAlert(AlertData(message: service.errorMessage));
    }
  }

  Future<bool> verifyOtpToken(String token) async {
    appState = AppState.isolatedBusy;
    final service = RequestService(
      RequestHelper.otp,
      body: {
        'code': token,
        'username': username,
      },
      useAutherization: false,
    );
    await service.post();

    if (service.isSuccess) {
      appState = AppState.idle;
      _verificationCode = token;
      return true;
    } else {
      appState = AppState.idle;

      if (service.statusCode != 409) {
        ActionSheetUtils(navigatorKey.currentContext!)
            .showAlert(AlertData(message: service.errorMessage));
      }
      if (service.statusCode != 429) {
        appState = AppState.idle;
      }
      return false;
    }
  }

  Future<bool> changePassword(UserProvider userProvider) async {
    appState = AppState.isolatedBusy;
    final service = RequestService(
      RequestHelper.changePassword,
      body: {
        'code': _verificationCode,
        'username': username,
        'password': newPassword,
      },
      useAutherization: false,
    );
    await service.put();

    if (service.isSuccess) {
      appState = AppState.idle;
      ActionSheetUtils(navigatorKey.currentContext!).showAlert(AlertData(
          aLertType: ALertType.success,
          message: 'رمز عبور با موفقیت تغییر کرد'));
      return true;
    } else {
      appState = AppState.failed;
      ActionSheetUtils(navigatorKey.currentContext!)
          .showAlert(AlertData(message: service.errorMessage));
      return false;
    }
  }
}
