package org.ntlab.citrusserver.repositories;
import org.ntlab.citrusserver.entities.Account;
import org.ntlab.citrusserver.entities.Book;
import org.ntlab.citrusserver.entities.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
@Repository
public class BookManager {
private final HashMap<String, HashMap<Integer, Book>> booksMap = new HashMap<>();
private final AccountManager accountManager; //仮
private final List<IBookListener> iBookListeners = new ArrayList<>();
@Autowired
public BookManager(AccountManager accountManager)
{
this.accountManager = accountManager;
createBook("fish","Aさんのダミ本","#B2A9F0", false);//ダミーの本
createBook("bird","Bさんのダミ本","#C1F8E5", false);//ダミーの本
}
//IBookListenerを追加
public void addListener(IBookListener iBookListener)
{
iBookListeners.add(iBookListener);
}
//本の一覧を返す
public HashMap<Integer, Book> getBooks(String accountId)
{
return booksMap.get(accountId);
}
//本の新規作成
public Book createBook(String accountId, String title, String color, Boolean publicity)
{
if(!booksMap.containsKey(accountId)){
booksMap.put(accountId, new HashMap<>());
}
Account account = accountManager.getAccount(accountId); //アカウントの取得
int newBookId = account.getNewBookId(); //新たに生成されたIdを取得(作成数もここで加算している)
String setTitle = title;
String setColor = color;
if(Objects.equals(title, "")) setTitle = "無名の本";
if(Objects.equals(color, "")) setColor = "#000000";
Book book = new Book(accountId, newBookId, setTitle, publicity, setColor, getDateTime()); //本の初期化
notifyChangedListener(account, book);//公開設定を通知します
booksMap.get(accountId).put(newBookId, book); //ブックに追加
return booksMap.get(accountId).get(newBookId); //(int->Bookを返すように変更した)
}
//本の情報を取得
public Book getBook(String accountId, Integer bookId)
{
return booksMap.get(accountId).get(bookId);
}
//本の削除
public void deleteBook(String accountId, Integer bookId)
{
Account account = accountManager.getAccount(accountId);
Book book = booksMap.get(accountId).get(bookId);
notifyDeletedListener(account, book);
booksMap.get(accountId).remove(bookId);
}
//((( いいねは省略 )))
//本のタイトルを返す
public String getTitle(String accountId, Integer bookId)
{
if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない
return booksMap.get(accountId).get(bookId).getTitle();
}
//本のタイトルを変更
public void putTitle(String accountId, Integer bookId, String title)
{
String setTitle = title;
if(Objects.equals(title, "")) setTitle = "無名の本";
booksMap.get(accountId).get(bookId).setTitle(setTitle);
}
//本の公開情報を返す
public Boolean getPublicity(String accountId, Integer bookId)
{
return booksMap.get(accountId).get(bookId).getPublicity();
}
//公開情報を変更する
public void putPublicity(String accountId, Integer bookId, Boolean publicity)
{
Account account = accountManager.getAccount(accountId);
Book book = booksMap.get(accountId).get(bookId);
if(book.getPublicity() == publicity) return;//変更が無ければ終了
book.setPublicity(publicity);
notifyChangedListener(account, book);
}
public void deleteAllBooks(String accountId)
{
booksMap.remove(accountId);
}
//((( 目標・振り返りは省略 )))
//本の色を変更する
public void putColor(String accountId, Integer bookId, String color)
{
booksMap.get(accountId).get(bookId).setColor(color);
}
//---------------------------------------------------------------------------------
// private
//---------------------------------------------------------------------------------
//IBookListenerに通知する
private void notifyChangedListener(Account account, Book book)
{
for(IBookListener iBookListener : iBookListeners)
{
iBookListener.bookChanged(account, book);
}
}
private void notifyDeletedListener(Account account, Book book)
{
for(IBookListener iBookListener : iBookListeners)
{
iBookListener.bookDeleted(account, book);
}
}
//現在時刻の取得
private String getDateTime()
{
LocalDateTime nowDate = LocalDateTime.now();
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
return dtf1.format(nowDate);
}
}