import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:yimaru_app/app/app.router.dart';
import '../../../app/app.locator.dart';
import 'onboarding_view.form.dart';

class OnboardingViewModel extends FormViewModel {
  final _navigationService = locator<NavigationService>();

  int _currentPage = 0;

  int get currentPage => _currentPage;

  int _previousPage = 0;

  int get previousPage => _previousPage;

  // Full name
  bool _focusFullName = false;

  bool get focusFullName => _focusFullName;

  // Educational background
  final List<String> _educationalBackgrounds = [
    'No formal education',
    'Primary school',
    'Secondary /High school',
    'College / Diploma',
    'Bachelor’s and above'
  ];

  List<String> get educationalBackgrounds => _educationalBackgrounds;

  String? _selectedEducationalBackground;

  String? get selectedEducationalBackground => _selectedEducationalBackground;

  // Age group
  final List<String> _ageGroups = [
    '8-14',
    '15-18',
    '19-26',
    '26+',
  ];

  List<String> get ageGroups => _ageGroups;

  String? _selectedAgeGroup;

  String? get selectedAgeGroup => _selectedAgeGroup;

  // Occupation
  bool _focusOccupation = false;

  bool get focusOccupation => _focusOccupation;

  // Learning goal
  String? _selectedLearningGoal;

  String? get selectedLearningGoal => _selectedLearningGoal;

  final List<Map<String, dynamic>> _learningGoals = [
    {
      'icon': 0,
      'title': 'Learn to Speak English',
      'subtitle': 'I know some English, but i want to learn to speak it',
    },
    {
      'icon': 1,
      'title': 'Practice Speaking English',
      'subtitle': 'I already speak English, but I want more practice.',
    },
    {
      'icon': 2,
      'title': 'Skill-based Courses',
      'subtitle': 'I want courses for IELTS, TOEFL, Duolingo, or work.',
    },
  ];

  List<Map<String, dynamic>> get learningGoals => _learningGoals;

  // Learning reason
  bool _showReasonTextBox = false;

  bool get showReasonTextBox => _showReasonTextBox;

  bool _focusLearningReason = false;

  bool get focusLearningReason => _focusLearningReason;

  String? _selectedLearningReason;

  String? get selectedLearningReason => _selectedLearningReason;

  final List<String> _learningReasons = [
    'Speak confidently at work or school',
    'Travel or handle daily situations',
    'Connect with family or friends',
    'General skills expansion',
    'Other'
  ];

  List<String> get learningReasons => _learningReasons;

  // Challenges
  bool _showChallengeTextBox = false;

  bool get showChallengeTextBox => _showChallengeTextBox;

  bool _focusChallenge = false;

  bool get focusChallenge => _focusChallenge;

  String? _selectedChallenge;

  String? get selectedChallenge => _selectedChallenge;

  final List<String> _challenges = [
    'Pronunciation',
    'Finding words or grammar quickly',
    'Feeling nervous or lacking confidence',
    'Understanding accents or fast speech',
    'Other'
  ];

  List<String> get challenges => _challenges;

  // Topic
  bool _showTopicTextBox = false;

  bool get showTopicTextBox => _showTopicTextBox;

  bool _focusTopic = false;

  bool get focusTopic => _focusTopic;

  String? _selectedTopic;

  String? get selectedTopic => _selectedTopic;

  final List<String> _topics = [
    'Food & Cooking',
    ' Hobbies, Sports, Music',
    'Tech, News, Business',
    'Travel, Places, Culture',
    'Other'
  ];

  List<String> get topics => _topics;

  // First assessment
  bool _focusFirstAssessment = false;

  bool get focusFirstAssessment => _focusFirstAssessment;

  // Second assessment
  final List<String> _secondAnswers = [
    'go',
    'goes',
    'went',
    'going',
  ];

  List<String> get secondAnswers => _secondAnswers;

  String? _selectedA2Answer;

  String? get selectedA2Answer => _selectedA2Answer;

  // Third assessment
  final List<String> _thirdAnswers = [
    'reduce',
    'grow',
    'stop',
    'hide',
  ];

  List<String> get thirdAnswers => _thirdAnswers;

  String? _selectedA3Answer;

  String? get selectedA3Answer => _selectedA3Answer;

  // Third assessment
  final List<String> _fourthAnswers = [
    'reduce',
    'grow',
    'stop',
    'hide',
  ];

  List<String> get fourthAnswers => _fourthAnswers;

  String? _selectedA4Answer;

  String? get selectedA4Answer => _selectedA4Answer;

  // Languages
  final List<Map<String, dynamic>> _languages = [
    {'code': 'አማ', 'language': 'አማርኛ'},
    {'code': 'EN', 'language': 'English'},
  ];

  List<Map<String, dynamic>> get languages => _languages;

  Map<String, dynamic> _selectedLanguage = {
    'code': 'EN',
    'language': 'English'
  };

  Map<String, dynamic> get selectedLanguage => _selectedLanguage;

  // Full name
  void setFullNameFocus() {
    _focusFullName = true;
    rebuildUi();
  }

  // Education background
  void setSelectedEducationalBackground(String title) {
    _selectedEducationalBackground = title;
    rebuildUi();
  }

  bool isSelectedEducationalBackground(String title) =>
      _selectedEducationalBackground == title;

  // Age group
  void setSelectedAgeGroup(String title) {
    _selectedAgeGroup = title;
    rebuildUi();
  }

  bool isSelectedAgeGroup(String title) => _selectedAgeGroup == title;

  // Occupation
  void setOccupationFocus() {
    _focusOccupation = true;
    rebuildUi();
  }

  // Country
  Future<List<String>> getCountries() async => ['Ethiopia', 'Djibouti'];

  // Region
  Future<List<String>> getRegions(String country) async =>
      ['Addis Ababa', 'Oromia'];

  // Learning goal
  void setSelectedLearningGoal(String title) {
    _selectedLearningGoal = title;
    rebuildUi();
  }

  bool isSelectedLearningGoal(String title) => _selectedLearningGoal == title;

  // Learning reason
  void setLearningReasonFocus() {
    _focusLearningReason = true;
    rebuildUi();
  }

  void setSelectedLearningReason(String title) {
    _selectedLearningReason = title;
    if (title.toLowerCase() == 'other') {
      _showReasonTextBox = true;
    } else {
      if (_showReasonTextBox) {
        _showReasonTextBox = false;
        _focusLearningReason = false;
      }
    }
    rebuildUi();
  }

  bool isSelectedLearningReason(String title) =>
      _selectedLearningReason == title;

  // Challenges
  void setChallengesFocus() {
    _focusChallenge = true;
    rebuildUi();
  }

  void setSelectedChallenge(String title) {
    _selectedChallenge = title;
    if (title.toLowerCase() == 'other') {
      _showChallengeTextBox = true;
    } else {
      if (_showChallengeTextBox) {
        _showChallengeTextBox = false;
        _focusChallenge = false;
      }
    }
    rebuildUi();
  }

  bool isSelectedChallenge(String title) => _selectedChallenge == title;

  // Topics
  void setTopicsFocus() {
    _focusTopic = true;
    rebuildUi();
  }

  void setSelectedTopic(String title) {
    _selectedTopic = title;
    if (title.toLowerCase() == 'other') {
      _showTopicTextBox = true;
    } else {
      if (_showTopicTextBox) {
        _showTopicTextBox = false;
        _focusTopic = false;
      }
    }
    rebuildUi();
  }

  bool isSelectedTopic(String title) => _selectedTopic == title;

  // First assessment
  void setFirstAssessmentFocus() {
    _focusFirstAssessment = true;
    rebuildUi();
  }

  // Second assessment
  void setSelectedA2Answer(String title) {
    _selectedA2Answer = title;
    rebuildUi();
  }

  bool isSelectedA2Answer(String title) => _selectedA2Answer == title;

  // Third assessment
  void setSelectedA3Answer(String title) {
    _selectedA3Answer = title;
    rebuildUi();
  }

  bool isSelectedA3Answer(String title) => _selectedA3Answer == title;

  // Fourth assessment
  void setSelectedA4Answer(String title) {
    _selectedA4Answer = title;
    rebuildUi();
  }

  bool isSelectedA4Answer(String title) => _selectedA4Answer == title;

  // Language
  void setSelectedLanguage(Map<String, dynamic> title) {
    _selectedLanguage = title;
    rebuildUi();
  }

  bool isSelectedLanguage(String title) =>
      _selectedLanguage['language'] == title;

  void next({int? page}) async {
    if (page == null) {
      if (_previousPage != 0) {
        _currentPage = _previousPage;
      } else {
        _currentPage++;
        if (_currentPage == 19) {
          rebuildUi();
          await Future.delayed(const Duration(seconds: 3));
          _currentPage++;
        }
      }
    } else {
      _previousPage = _currentPage;
      _currentPage = page;
    }
    rebuildUi();
  }

  void pop({bool language = false}) {
    if (!language) {
      _currentPage--;
    } else {
      _currentPage = _previousPage;
      _previousPage = 0;
    }

    rebuildUi();
  }
}
