#pragma once
// Spec: shell.md section 4 (AppState, AppState+Vote, AppState+React). Singleton UI-thread state. Every mutation
// bumps version() and notifies subscribers (Observable). DIFF: updateAppIconBadge (APNs/UNUserNotificationCenter)
// is not ported, there is no icon badge on desktop. The feed is capped at ModelLimits::kMaxFeedPosts.
#include <functional>
#include <optional>
#include <string>
#include "api/ApiTypes.hpp"
#include "core/Observable.hpp"
#include "model/AuthToken.hpp"
#include "model/Feed.hpp"
#include "model/TargetType.hpp"
#include "model/User.hpp"
#include "model/Vote.hpp"
namespace dp {
class DevPlaceApi;
class AppState : public Observable {
public:
static AppState& instance();
const std::optional<AuthToken>& token() const { return token_; }
bool isLoggedIn() const { return token_.has_value(); }
const std::optional<User>& currentUser() const { return currentUser_; }
const std::optional<Feed>& feed() const { return feed_; }
std::int64_t unreadNotificationCount() const { return unreadNotificationCount_; }
std::int64_t unreadMessageCount() const { return unreadMessageCount_; }
const std::optional<std::string>& pendingConversationUid() const { return pendingConversationUid_; }
void setToken(std::optional<AuthToken> value);
void setCurrentUser(std::optional<User> value);
void setFeed(std::optional<Feed> value);
void setUnreadCounts(std::int64_t notifications, std::int64_t messages);
void setPendingConversationUid(std::optional<std::string> value);
bool isCurrentUser(const std::string& id) const { return currentUser_ && currentUser_->id == id; }
// First page of the feed, then unread counts. A feed failure leaves the feed untouched and is reported.
void loadFeed(DevPlaceApi& api, Done<Unit> done);
// Errors are logged ("Failed to load unread counts: ..."), never reported. `done` runs afterwards.
void loadUnreadCounts(DevPlaceApi& api, std::function<void()> done = {});
// api.messages(withUid).otherUser, nullopt on error (logged).
void conversationUser(const std::string& uid, DevPlaceApi& api, std::function<void(std::optional<User>)> done);
// Needs a feed with nextCursor, else completes ok at once. Duplicates (by post id) are dropped and logged.
void loadMoreFeed(DevPlaceApi& api, Done<Unit> done);
// token, feed, currentUser nil; counts 0; pendingConversationUid nil.
void clear();
void updatePostVoteInFeed(const std::string& postId, Vote vote, std::int64_t count);
void updateCommentVoteInFeed(const std::string& commentId, Vote vote);
void updatePostReactionInFeed(const std::string& postId, const std::string& emoji);
void updateCommentReactionInFeed(const std::string& commentId, const std::string& emoji);
// Optimistic upvote toggle: apply(newVote) now (up -> none, otherwise up), api.vote(.up) always, on error
// apply(currentVote) and a log line. `done` runs after the request finished (success or not).
void performVoteToggle(TargetType targetType, const std::string& targetId, Vote currentVote, DevPlaceApi& api,
std::function<void(Vote)> apply, std::function<void()> done = {});
// Optimistic reaction toggle: apply() now, api.react, on error apply() again (toggles back) and a log line.
void performReactionToggle(TargetType targetType, const std::string& targetId, const std::string& emoji, DevPlaceApi& api,
std::function<void()> apply, std::function<void()> done = {});
private:
AppState() = default;
std::optional<AuthToken> token_;
std::optional<User> currentUser_;
std::optional<Feed> feed_;
std::int64_t unreadNotificationCount_ = 0;
std::int64_t unreadMessageCount_ = 0;
std::optional<std::string> pendingConversationUid_;
};
} // namespace dp