﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZocMonGraph.App
{
    public static class IntervalHelper
    {
        public static Resolution GetResolutionFromString(string resolutionStr)
        {
            switch (resolutionStr)
            {
                case "minutely":
                    return Resolution.Minutely;
                case "fiveMinutely":
                    return Resolution.FiveMinutely;
                case "hourly":
                    return Resolution.Hourly;
                case "daily":
                    return Resolution.Daily;
                default:
                    throw new Exception("Invalid resolution.");
            }
        }

        public static long GetLineInterval(string historicalRange)
        {
            int multiplier;
            long unit;

            if (historicalRange.ToLower().Contains("one"))
                multiplier = 1;
            else if (historicalRange.ToLower().Contains("two"))
                multiplier = 2;
            else if (historicalRange.ToLower().Contains("three"))
                multiplier = 3;
            else if (historicalRange.ToLower().Contains("four"))
                multiplier = 4;
            else if (historicalRange.ToLower().Contains("eight"))
                multiplier = 8;
            else
                throw new Exception("Invalid Historical Range");

            if (historicalRange.ToLower().Contains("hour"))
                unit = 60;
            else if (historicalRange.ToLower().Contains("day"))
                unit = 1440;
            else if (historicalRange.ToLower().Contains("week"))
                unit = 10080;
            else if (historicalRange.ToLower().Contains("month"))
            {
                var now = DateTime.Now;
                var monthsAgo = now.AddMonths(-multiplier);
                var difference = now - monthsAgo;
                return (long) difference.TotalMinutes;
            }
            else
                throw new Exception("Invalid Historical Range");

            return unit * multiplier;
        }


        public static PreviousUnit GetIntervalUnitFromString(string unitStr)
        {
            if (unitStr.ToLower().Contains("day"))
                return PreviousUnit.Day;
            if (unitStr.ToLower().Contains("week"))
                return PreviousUnit.Week;
            if (unitStr.ToLower().Contains("month"))
                return PreviousUnit.Month;

            throw new Exception("Invalid show previous.");
        }

        public static List<PreviousMagnitude> GetIntervalMagnitudeFromString(string magnitudeStr)
        {

            if (magnitudeStr.ToLower().Contains("one"))
                return new List<PreviousMagnitude>() {PreviousMagnitude.One};
            if (magnitudeStr.ToLower().Contains("two"))
                return new List<PreviousMagnitude>() { PreviousMagnitude.One, PreviousMagnitude.Two};
            if (magnitudeStr.ToLower().Contains("three"))
                return new List<PreviousMagnitude>() { PreviousMagnitude.One, PreviousMagnitude.Two, PreviousMagnitude.Three };
            if (magnitudeStr.ToLower().Contains("four"))
                return new List<PreviousMagnitude>() { PreviousMagnitude.One, PreviousMagnitude.Two, PreviousMagnitude.Three, PreviousMagnitude.Four };
            
            throw new Exception("Invalid show previous.");
        } 

        public static List<PreviousMagnitude> GetIntervalUnitFromCustom(List<string> customStr)
        {
            var magList = new List<PreviousMagnitude>();

            if (customStr.Contains("one"))
                magList.Add(PreviousMagnitude.One);
            if (customStr.Contains("two"))
                magList.Add(PreviousMagnitude.Two);
            if (customStr.Contains("three"))
                magList.Add(PreviousMagnitude.Three);
            if (customStr.Contains("four"))
                magList.Add(PreviousMagnitude.Four);
            return magList;
        }

        public static long GetMinutesInIntervalUnit(PreviousUnit unit)
        {
                switch (unit)
                {
                    case PreviousUnit.Day:
                        return 1440;
                    case PreviousUnit.Week:
                        return 10080;
                    case PreviousUnit.Month:
                        return 43829;
                    default:
                        throw new Exception("Invalid unit of time.");
            } 
        }
    }

    [Flags]
    public enum PreviousUnit
    {
        Day, 
        Week, 
        Month 
    }

    [Flags]
    public enum PreviousMagnitude
    {
        One = 1,
        Two = 2,
        Three = 3,
        Four = 4 
    }

    [Flags]
    public enum Resolution 
    {
        Minutely = 60000,
        FiveMinutely = 300000,
        Hourly = 3600000,
        Daily = 86400000
    }
}
