using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CBR.Core.Models;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using CBR.Core.Helpers;
using System.Windows;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Media.Imaging;
namespace CBR.Core.Services
{
public class BookServiceBase
{
#region -----------------BOOK INFO-----------------
public Book CreateBook(string filePath)
{
return new Book(null, filePath);
}
public Book CreateBookWithCover(string folder, FileInfo file)
{
string fileInfo = Path.Combine(folder, file.DirectoryName.Replace(file.Directory.Root.Name, "").Replace('\\', '.') + "." + file.Name + ".cbb");
if (File.Exists(fileInfo))
{
return new BookInfoService().LoadBookInfo(fileInfo);
}
else
{
Book bk = new Book(fileInfo, file.FullName);
bk.Size = file.Length / 1024 / 1024;
Thread t = new Thread(new ParameterizedThreadStart(LoadCoverThread));
t.IsBackground = true;
t.Priority = ThreadPriority.Lowest;
t.Start(bk);
return bk;
}
}
virtual internal void LoadCoverThread(object param)
{
throw new NotImplementedException();
}
virtual public object LoadBook(Book bk)
{
WorkspaceService.Instance.AddRecent(bk);
return null;
}
virtual public void UnloadBook(Book bk)
{
bk.Pages.Clear();
}
#endregion
#region -----------------BOOK-----------------
virtual public void EditBook(Book bk)
{
throw new NotImplementedException();
}
virtual public Book SaveBook(Book bk)
{
throw new NotImplementedException();
}
public bool IsDynamic(Book bk)
{
try
{
if( bk != null )
return (bk.Pages.AsParallel().Count(p => p.Frames.Count != 0) != 0);
}
catch (Exception err)
{
ExceptionHelper.Manage("BookServiceBase:IsDynamic", err);
}
return false;
}
public void SynchronizeProperties(Book bk)
{
try
{
IDictionary<string, object> dict = (IDictionary<string, object>)bk.Dynamics;
// add the properties from the settings if missing
foreach( string k in WorkspaceService.Instance.Settings.Dynamics )
{
if (!dict.Keys.Contains(k))
dict.Add(k, string.Empty);
}
// remove old properties that were removed from settings
foreach (string k in dict.Keys)
{
if (!WorkspaceService.Instance.Settings.Dynamics.Contains(k))
dict.Keys.Remove(k);
}
}
catch (Exception err)
{
ExceptionHelper.Manage("BookServiceBase:SynchronizeProperties", err);
}
}
#endregion
#region -----------------BOOKMARK-----------------
virtual public void SetMark(Book bk, Page pg)
{
throw new NotImplementedException();
}
virtual public Page GotoMark(Book bk)
{
throw new NotImplementedException();
}
virtual public void ClearMark(Book bk)
{
if (bk != null)
bk.Bookmark = string.Empty;
}
virtual public bool HasMark(Book bk)
{
if (bk != null)
return !string.IsNullOrEmpty(bk.Bookmark);
else
return false;
}
#endregion
#region -----------------PAGE NAVIGATION-----------------
virtual public Page GotoPage(Book bk, Page currentPage, int step)
{
int pos = 0;
if (currentPage != null)
pos = bk.Pages.IndexOf(currentPage);
if (step == -1)
{
return (pos == 0) ? currentPage : bk.Pages[pos - 1];
}
else
{
return (pos >= bk.Pages.Count - 1) ? currentPage : bk.Pages[pos + 1];
}
}
virtual public bool CheckPageRange(Book bk, Page currentPage, int step)
{
int pos = 0;
if (currentPage != null)
pos = bk.Pages.IndexOf(currentPage);
if (step == -1)
{
return (pos == 0) ? false : true;
}
else
{
return (pos >= bk.Pages.Count - 1) ? false : true;
}
}
public void GotoFrame(Book bk, ref Page currentPage, ref Zone currentFrame, int step)
{
currentFrame = GotoFrame(currentPage, currentFrame, step);
//we reach the min or max, change the page
if (currentFrame == null)
currentPage = GotoPage(bk, currentPage, step);
GotoFrame(currentPage, currentFrame, step);
}
internal Zone GotoFrame(Page currentPage, Zone currentFrame, int step)
{
//if no frames, return a page frame
if (currentPage.Frames.Count <= 0)
{
if (currentFrame == null)
return new Zone() { Duration = 15, OrderNum = 0, X = 0, Y = 0, Type = FrameType.Page };
else
return null;
}
//search current frame position
int posFrame = 0;
if (currentFrame != null)
posFrame = currentPage.Frames.IndexOf(currentFrame);
//we go back, manage the min
if (step == -1)
{
return (posFrame == 0) ? null : currentPage.Frames[posFrame - 1];
}
else //we go forward, manage the max
{
return (posFrame >= currentPage.Frames.Count - 1) ? null : currentPage.Frames[posFrame + 1];
}
}
#endregion
virtual public void Delete(Book bk)
{
try
{
//delete the book
File.Delete(bk.FilePath);
//delete the bin
File.Delete(bk.BookInfoFilePath);
}
catch (Exception err)
{
ExceptionHelper.Manage("BookServiceBase:Delete", err);
}
}
virtual public void Protect(Book bk, bool status, string password)
{
if (bk != null)
{
if (status == false) //remove protection
{
if (bk.Password != password)
return;
}
bk.Password = password;
bk.IsSecured = status;
}
}
virtual public long ManageCache(Book bk)
{
return 0;
}
}
}