using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using TradeIdeas.TIProData;
using TradeIdeas.XML;
namespace TradeIdeas.TIProGUI
{
///
/// Stores all browser charts drawing tools cache. Knows how to save to and restore from XML.
///
public class BrowserChartDrawingToolsCache
{
public BrowserChartDrawingToolsCache() { }
///
/// Adds an annotation to the cache.
///
///
///
public void AddAnnotation(CacheableBrowserChartDrawingTools annotation, string symbol)
{
if (annotation == null)
{
return;
}
List annotations = new List();
if (_annotations.ContainsKey(symbol))
annotations = _annotations[symbol];
var existingAnnotationIndex = annotations.FindIndex(x => x.ChartTimeframe == annotation.ChartTimeframe);
if (existingAnnotationIndex > -1)
{
annotations[existingAnnotationIndex] = annotation;
}
else
{
annotations.Add(annotation);
}
_annotations[symbol] = annotations;
}
///
/// Gets all annotations from the cache that correspond to the symbol and timeframe. Horizontal lines span all timeframes but all other
/// annotation types are specific to a timeframe.
///
///
///
///
public CacheableBrowserChartDrawingTools GetAnnotation(string symbol, ChartTimeframe timeframe)
{
List annotationsInScope = new List();
if (_annotations.ContainsKey(symbol))
annotationsInScope = _annotations[symbol];
if (annotationsInScope.DoesNotHaveAny())
{
return new CacheableBrowserChartDrawingTools();
}
var foundAnnotation = annotationsInScope.FirstOrDefault(x => x.ChartTimeframe == timeframe);
return foundAnnotation != null ? foundAnnotation : new CacheableBrowserChartDrawingTools();
}
///////
/////// Returns CacheableBrowserChartAnnotation by looking up the original id used when it was created.
///////
///////
///////
/////// CacheableBrowserChartAnnotation object.
////public CacheableBrowserChartDrawingTools GetAnnotation(string id, string symbol)
////{
//// if (_annotations.ContainsKey(symbol))
//// {
//// List annotations = _annotations[symbol].Where(x => x.Id == id).ToList();
//// if (annotations.Count > 0)
//// return annotations.First();
//// }
//// return null;
////}
///////
/////// Removes annotation from cache by first looking it up by id and symbol. If it can't be found this does nothing.
///////
///////
///////
////public void RemoveAnnotation(string id, string symbol)
////{
//// if (_annotations.ContainsKey(symbol))
//// {
//// List annotations = _annotations[symbol].Where(x => x.Id == id).ToList();
//// if (annotations.Count > 0)
//// {
//// foreach (CacheableBrowserChartDrawingTools annotation in annotations)
//// {
//// _annotations[symbol].Remove(annotation);
//// }
//// }
//// }
////}
///////
/////// Removes annotation from cache by first looking it up by symbol. If it can't be found this does nothing.
/////// Returns the list of Ids that has been removed
///////
///////
////public List RemoveAllAnnotation(string symbol)
////{
//// List removed = new List();
//// if (_annotations.ContainsKey(symbol))
//// {
//// List annotations = _annotations[symbol].ToList();
//// foreach (CacheableBrowserChartDrawingTools annotation in annotations)
//// {
//// removed.Add(annotation.Id);
//// _annotations[symbol].Remove(annotation);
//// }
//// }
//// return removed;
////}
///
/// Data structure for storing all annotations.
///
private Dictionary> _annotations = new Dictionary>();
///
/// Caches all current annotations to XML.
///
/// XML string that can be used in Restore.
public string Save()
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("");
XmlNode annotationsNode = doc.Node("BROWSER_CHARTS_ANNOTATIONS");
foreach (string symbol in _annotations.Keys)
{
XmlNode symbolNode = annotationsNode.NewNode("SYMBOL");
symbolNode.SetProperty("SYMBOL", symbol);
foreach (CacheableBrowserChartDrawingTools annotation in _annotations[symbol])
{
annotation.Save(symbolNode);
}
}
return doc.OuterXml;
}
catch (Exception e)
{
string debugView = e.StackTrace;
return "";
}
}
///
/// Restores annotations from XML.
///
///
public void Restore(string xml)
{
try
{
_annotations.Clear();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode annotationsNode = doc.Node(0);
foreach (XmlNode symbolNode in annotationsNode.ChildNodes)
{
string symbol = symbolNode.Property("SYMBOL", "");
List annotations = new List();
foreach (XmlNode annotationNode in symbolNode.ChildNodes)
{
CacheableBrowserChartDrawingTools annotation = new CacheableBrowserChartDrawingTools();
annotation.Restore(annotationNode);
annotations.Add(annotation);
}
_annotations.Add(symbol, annotations);
}
}
catch { }
}
}
///
/// Generic object to cache a web browser drawing tools
///
public class CacheableBrowserChartDrawingTools
{
public CacheableBrowserChartDrawingTools() { }
public ChartTimeframe ChartTimeframe { get; set; }
public string Content { get; set; } = string.Empty;
////public override bool Equals(object obj)
////{
//// CacheableBrowserChartAnnotation CacheableBrowserChartAnnotation = obj as CacheableBrowserChartAnnotation;
//// if (null == CacheableBrowserChartAnnotation)
//// return false;
//// return CacheableBrowserChartAnnotation.Id == Id;
////}
public override int GetHashCode()
{
return base.GetHashCode();
}
public void Save(XmlNode parent)
{
XmlNode newNode = parent.NewNode("BROWSER_CHARTS_ANNOTATION");
newNode.SetProperty("TIMEFRAME", ChartTimeframe);
newNode.InnerText = Content;
}
public void Restore(XmlNode description)
{
ChartTimeframe = description.PropertyEnum("TIMEFRAME", ChartTimeframe.Daily);
Content = description.InnerText;
}
}
}