import 'package:flutter/material.dart';

import 'package:pshared/provider/account.dart';

import 'package:pweb/models/edit_state.dart';


class AccountNameState extends ChangeNotifier {
  AccountNameState({
    required this.initialFirstName,
    required this.initialLastName,
    required this.errorMessage,
    required AccountProvider accountProvider,
  }) : _accountProvider = accountProvider {
    _firstNameController = TextEditingController(text: initialFirstName);
    _lastNameController = TextEditingController(text: initialLastName);
  }

  final AccountProvider _accountProvider;
  final String initialFirstName;
  final String initialLastName;
  final String errorMessage;

  late final TextEditingController _firstNameController;
  late final TextEditingController _lastNameController;
  EditState _editState = EditState.view;
  String _errorText = '';
  bool _disposed = false;

  TextEditingController get firstNameController => _firstNameController;
  TextEditingController get lastNameController => _lastNameController;
  EditState get editState => _editState;
  String get errorText => _errorText;
  bool get isEditing => _editState != EditState.view;
  bool get isSaving => _editState == EditState.saving;
  bool get isBusy => _accountProvider.isLoading || isSaving;
  String get currentFirstName => _accountProvider.account?.name ?? initialFirstName;
  String get currentLastName => _accountProvider.account?.lastName ?? initialLastName;
  String get currentFullName {
    final first = currentFirstName.trim();
    final last = currentLastName.trim();
    if (first.isEmpty && last.isEmpty) return '';
    if (first.isEmpty) return last;
    if (last.isEmpty) return first;
    return '$first $last';
  }

  void startEditing() => _setState(EditState.edit);

  void cancelEditing() {
    _firstNameController.text = currentFirstName;
    _lastNameController.text = currentLastName;
    _setError('');
    _setState(EditState.view);
  }

  void syncNames(String latestFirstName, String latestLastName) {
    if (isEditing) return;
    if (_firstNameController.text != latestFirstName) {
      _firstNameController.text = latestFirstName;
    }
    if (_lastNameController.text != latestLastName) {
      _lastNameController.text = latestLastName;
    }
  }

  Future<bool> save() async {
    final newFirstName = _firstNameController.text.trim();
    final newLastName = _lastNameController.text.trim();
    final currentFirst = currentFirstName;
    final currentLast = currentLastName;

    if (newFirstName.isEmpty || (newFirstName == currentFirst && newLastName == currentLast)) {
      cancelEditing();
      return false;
    }

    _setError('');
    _setState(EditState.saving);

    try {
      await _accountProvider.resetUsername(newFirstName, lastName: newLastName);
      _setState(EditState.view);
      return true;
    } catch (_) {
      _setError(errorMessage);
      _setState(EditState.edit);
      return false;
    } finally {
      if (_editState == EditState.saving) {
        _setState(EditState.edit);
      }
    }
  }

  void _setState(EditState value) {
    if (_disposed || _editState == value) return;
    _editState = value;
    notifyListeners();
  }

  void _setError(String value) {
    if (_disposed) return;
    _errorText = value;
    notifyListeners();
  }

  @override
  void dispose() {
    _disposed = true;
    _firstNameController.dispose();
    _lastNameController.dispose();
    super.dispose();
  }
}
