import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../../../app/app.logger.dart';
import '../../../app/app.router.dart';
import '../../../app/core/custom_base_view_model.dart';
import '../../../app/enum/snackbar_enum.dart';
import '../../../model/ont_model.dart';

class LoginViewModel extends CustomBaseViewModel {
  final log = getLogger('LoginViewModel');

  // form variable
  final formKey = GlobalKey<FormState>();
  TextEditingController nomorInternetController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  Future<void> init() async {}

  login() async {
    log.wtf('login proses');
    easyLoading.customLoading("Login...");
    setBusy(true);

    try {
      var formData = FormData.fromMap({
        "no_internet": nomorInternetController.text,
        "password": passwordController.text,
      });
      var res = await httpService.postWithFormData(
        'login',
        formData,
      );

      // log.wtf(res.statusCode);
      if (res.statusCode == 200) {
        // log.wtf(res.data["data"]['id']);
        OntModel ontModel = OntModel.fromJson(res.data["data"]);
        otherFunction.ontModel = ontModel;
        await mySharedPrefs.setString('id', ontModel.id.toString());
        // log.wtf(await mySharedPrefs.getString('id'));
        // log.wtf(ontModel.toJson());
        // snackbarService.showCustomSnackBar(
        //   message: "Login Berhasil",
        //   variant: SnackbarType.sukses,
        //   duration: const Duration(milliseconds: 2000),
        // );
        // await 2 seconds
        // await Future.delayed(const Duration(seconds: 2));
        navigationService.replaceWith(Routes.navBarView);
        snackbarService.showCustomSnackBar(
          message: "Login Berhasil",
          variant: SnackbarType.sukses,
          duration: const Duration(milliseconds: 10000),
        );
      }
    } catch (e) {
      return;
    } finally {
      setBusy(false);
      easyLoading.dismiss();
    }
  }
}
