

enum TraktScrobbleState { idle, started, paused, stopped }

enum TraktScrobbleAction { none, start, pause, stop }

class TraktScrobbleStateMachine {
  TraktScrobbleState _state = TraktScrobbleState.idle;

  TraktScrobbleState get state => _state;


  TraktScrobbleAction onPlay() {
    if (_state == TraktScrobbleState.started) return TraktScrobbleAction.none;
    if (_state == TraktScrobbleState.stopped) return TraktScrobbleAction.none;
    _state = TraktScrobbleState.started;
    return TraktScrobbleAction.start;
  }


  TraktScrobbleAction onPause() {
    if (_state != TraktScrobbleState.started) return TraktScrobbleAction.none;
    _state = TraktScrobbleState.paused;
    return TraktScrobbleAction.pause;
  }


  TraktScrobbleAction onCompleted() {
    if (_state == TraktScrobbleState.stopped) return TraktScrobbleAction.none;
    if (_state == TraktScrobbleState.idle) return TraktScrobbleAction.none;
    _state = TraktScrobbleState.stopped;
    return TraktScrobbleAction.stop;
  }


  TraktScrobbleAction onStop({required double progress}) {
    if (_state == TraktScrobbleState.idle ||
        _state == TraktScrobbleState.stopped) {
      return TraktScrobbleAction.none;
    }
    _state = TraktScrobbleState.stopped;
    if (progress < 1) return TraktScrobbleAction.none;
    return TraktScrobbleAction.stop;
  }
}
