import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:initial_folder/models/lesson_course_model.dart';
import 'package:initial_folder/models/section_model.dart';
import 'package:initial_folder/services/lesson_course_service.dart';

enum ResultState { loading, hasData, noData, error }
// enum ResultStateUpdate { uninitialized, loading, success, eror }

class LessonCourseProvider with ChangeNotifier {
  final LessonCourseService lessonCourseService;
  final int id;
  LessonCourseProvider({required this.lessonCourseService, required this.id}) {
    getLessonCourse(id);
  }

  bool switchbutton = false;

  LessonCourseModel? _lessonCourseModel;
  SectionModel? _sectionModel;
  ResultState? _state;
  // ResultStateUpdate _stateUpdate = ResultStateUpdate.uninitialized;
  String _message = '';

  LessonCourseModel? get result => _lessonCourseModel;
  SectionModel? get sectionResult => _sectionModel;

  ResultState? get state => _state;
  // ResultStateUpdate get stateUpdate => _stateUpdate;
  String get message => _message;

  String _url = '';

  String get url => _url;

  set uri(String urlVideo) {
    _url = urlVideo;
    notifyListeners();
  }

  indexUri(String indexUri) {
    _url = indexUri;
    notifyListeners();
  }

  set lessonCourse(LessonCourseModel lesson) {
    _lessonCourseModel = lesson;
    notifyListeners();
  }

  set sectionCourse(SectionModel section) {
    _sectionModel = section;
    notifyListeners();
  }
  // set newMap(NewMap lesson) {
  //   _newMap = lesson;
  //   notifyListeners();
  // }

  void autoplay() {
    switchbutton = !switchbutton;
    notifyListeners();
  }

  Future<dynamic> getLessonCourse(int _id) async {
    try {
      _state = ResultState.loading;
      notifyListeners();
      LessonCourseModel lesson = await lessonCourseService.getLessonCourse(_id);
      SectionModel section = await lessonCourseService.getSectionCourse(_id);

      if (lesson.data[0].isEmpty && section.data[0].isEmpty) {
        _state = ResultState.noData;
        notifyListeners();
        return _message = 'Empty Data';
      } else {
        _state = ResultState.hasData;

        notifyListeners();
        _sectionModel = section;
        return _lessonCourseModel = lesson;
      }
    } catch (e) {
      _state = ResultState.error;
      notifyListeners();
      print('masuk sini');
      return _message = 'Error --> $e';
    }
  }

  Future<bool> updateLessonCourse(String _courseId, String _lessonId) async {
    try {
      // _stateUpdate = ResultStateUpdate.loading;
      // notifyListeners();
      bool update = await lessonCourseService.updateLessonCourse(_lessonId);

      if (update) {
        // _stateUpdate = ResultStateUpdate.success;
        // notifyListeners();
        return true;
      }
      return false;
    } catch (e) {
      // _stateUpdate = ResultStateUpdate.eror;
      return false;
    }
  }
}
