using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using C1.Win.Chart.Finance;

using FinancialChartExplorer.Services;

namespace FinancialChartExplorer.Samples
{
    public partial class Indicators : UserControl
    {
        ATR atr = new ATR() { Name = "ATR" };
        RSI rsi = new RSI() { Name = "RSI" };
        CCI cci = new CCI() { Name = "CCI" };
        WilliamsR wi = new WilliamsR() { Name = "Williams %R" };

        public Indicators()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var dataService = DataService.GetService();
            var data = dataService.GetSymbolData("box");

            financialChart1.BeginUpdate();
            financialChart1.BindingX = "date";
            financialChart1.Binding = "close";
            financialChart1.Series.Add(new FinancialSeries());
            financialChart1.ChartType = C1.Chart.Finance.FinancialChartType.Line;
            financialChart1.DataSource = data;
            financialChart1.Rendered += (s, a) =>
                {
                    financialChart2.AxisX.Min = financialChart1.AxisX.ActualMin;
                    financialChart2.AxisX.Max = financialChart1.AxisX.ActualMax;
                };
            financialChart1.EndUpdate();

            financialChart2.BeginUpdate();
            financialChart2.ChartType = C1.Chart.Finance.FinancialChartType.Line;
            financialChart2.BindingX = "date";
            financialChart2.Binding = "high,low,close";
            financialChart2.Series.Add(atr);
            financialChart2.Legend.Position = C1.Chart.Position.Bottom;
            financialChart2.DataSource = data;
            financialChart2.EndUpdate();

            indicatorType.DataSource = new string[] 
            {
                "Average True Range",
                "Relative Strength Index",
                "Commodity Channel Index",
                "Williams %R"
            };

            period.Value = atr.Period;
        }

        private void indicatorType_SelectedIndexChanged(object sender, EventArgs e)
        {
            FinancialSeries ser = null;
            if (indicatorType.SelectedIndex == 0)
                ser = atr;
            else if (indicatorType.SelectedIndex == 1)
                ser = rsi;
            else if (indicatorType.SelectedIndex == 2)
                ser = cci;
            else if (indicatorType.SelectedIndex == 3)
                ser = wi;

            if (ser != null && !financialChart2.Series.Contains(ser))
            {
                financialChart2.Series.Clear();
                financialChart2.Series.Add(ser);
            }
        }

        private void period_ValueChanged(object sender, EventArgs e)
        {
            atr.Period = rsi.Period = cci.Period = (int)period.Value;
        }
    }
}
