import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/services/ntrip_service.dart';
import 'package:shared_preferences/shared_preferences.dart';

class NtripSettingsController extends GetxController {
  final formKey = GlobalKey<FormState>();

  final hostController = TextEditingController();
  final portController = TextEditingController();
  final mountPointController = TextEditingController();
  final usernameController = TextEditingController();
  final passwordController = TextEditingController();

  final isPasswordVisible = false.obs;
  final autoConnect = false.obs;
  final isBusy = false.obs;

  final _secureStorage = const FlutterSecureStorage();

  @override
  void onInit() {
    super.onInit();
    loadSettings();
  }

  Future<void> loadSettings() async {
    final prefs = await SharedPreferences.getInstance();

    hostController.text = prefs.getString('ntrip_host') ?? '';
    portController.text = prefs.getInt('ntrip_port')?.toString() ?? '2101';
    mountPointController.text = prefs.getString('ntrip_mountpoint') ?? '';
    usernameController.text = prefs.getString('ntrip_username') ?? '';
    autoConnect.value = prefs.getBool('ntrip_auto_connect') ?? false;

    passwordController.text =
        await _secureStorage.read(key: 'ntrip_password') ?? '';
  }

  Future<void> saveSettings() async {
    if (!(formKey.currentState?.validate() ?? false)) {
      return;
    }

    final prefs = await SharedPreferences.getInstance();

    await prefs.setString('ntrip_host', hostController.text.trim());
    await prefs.setInt(
      'ntrip_port',
      int.parse(portController.text.trim()),
    );
    await prefs.setString(
      'ntrip_mountpoint',
      mountPointController.text.trim(),
    );
    await prefs.setString(
      'ntrip_username',
      usernameController.text.trim(),
    );
    await prefs.setBool(
      'ntrip_auto_connect',
      autoConnect.value,
    );

    await _secureStorage.write(
      key: 'ntrip_password',
      value: passwordController.text,
    );
  }

  Future<void> saveAndConnect() async {
    if (!(formKey.currentState?.validate() ?? false)) {
      return;
    }

    try {
      isBusy.value = true;

      await saveSettings();

      // await NtripService.to.connect(
      //   host: hostController.text.trim(),
      //   port: int.parse(portController.text.trim()),
      //   mountPoint: mountPointController.text.trim(),
      //   username: usernameController.text.trim(),
      //   password: passwordController.text,
      // );

      Get.back();

      Get.snackbar(
        'NTRIP',
        'Kapcsolódás elindítva.',
        snackPosition: SnackPosition.BOTTOM,
      );
    } catch (e) {
      Get.snackbar(
        'NTRIP hiba',
        e.toString(),
        snackPosition: SnackPosition.BOTTOM,
      );
    } finally {
      isBusy.value = false;
    }
  }

  Future<void> disconnect() async {
    try {
      isBusy.value = true;
      await NtripService.to.disconnect();
    } finally {
      isBusy.value = false;
    }
  }

  @override
  void onClose() {
    hostController.dispose();
    portController.dispose();
    mountPointController.dispose();
    usernameController.dispose();
    passwordController.dispose();
    super.onClose();
  }
}
