HamBook – Rev 15

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HamBook.Radios.Generic
{
    public class RadioPhase
    {
        public int Phase { get; private set; }

        public RadioPhase(string phase)
        {
            Phase = Parse(phase);
        }

        public RadioPhase(int phase)
        {
            Phase = Parse(phase);
        }

        public static bool TryParse(string phase, out RadioPhase radioPhase)
        {
            switch (phase)
            {

                case "Simplex":
                case "Plus Shift":
                case "Minus Shift":
                    radioPhase = new RadioPhase(phase);
                    return true;
                default:
                    radioPhase = null;
                    return false;
            }
        }

        public static bool TryParse(int phase, out RadioPhase radioPhase)
        {
            switch (phase)
            {
                case 0:
                case 1:
                case 2:
                    radioPhase = new RadioPhase(phase);
                    return true;
                default:
                    radioPhase = null;
                    return false;
            }
        }

        private int Parse(string mode)
        {
            switch (mode)
            {
                case "Simplex":
                    return 0;
                case "Plus Shift":
                    return 1;
                case "Minus Shift":
                    return 2;
                default:
                    throw new ArgumentException();
            }
        }

        private int Parse(int phase)
        {
            switch (phase)
            {
                case 0:
                case 1:
                case 2:
                    return phase;
                default:
                    throw new ArgumentException();
            }
        }

        public static implicit operator RadioPhase(string phase)
        {
            return new RadioPhase(phase);
        }

        public static implicit operator string(RadioPhase radioPhase)
        {
            switch (radioPhase.Phase)
            {
                case 0:
                    return "Simplex";
                case 1:
                    return "Plus Shift";
                case 2:
                    return "Minus Shift";
                default:
                    throw new ArgumentException();
            }
        }

        public static implicit operator RadioPhase(int phase)
        {
            return new RadioPhase(phase);
        }

        public static implicit operator int(RadioPhase radioPhase)
        {
            return radioPhase.Phase;
        }
    }
}