using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Skyscraper
{
    public class SatellitePosition
    {
        public SatellitePosition()
        {
        }

        public SatellitePosition(float angle, int cardinalDirection, string name)
        {
            this.angle = angle;
            this.cardinalDirection = cardinalDirection;
            this.name = name;
        }

        public float angle;
        public int cardinalDirection;
        public string name;

        public int Checksum => GetChecksum(angle, cardinalDirection);

        public static int GetChecksum(float angle, int cardinal)
        {
            int result = (int)(angle * 10.0f);
            if (cardinal == 1)
	            result += 3600;
            return result;
        }
        
        public override string ToString()
        {
            return String.Format("{0:0.0}° {1} {2}", angle, cardinalDirection == 0 ? "E" : "W", name);
        }

        public static SatellitePosition FromChecksum(int checksum)
        {
	        bool isWest = false;
	        if (checksum >= 3600)
	        {
		        checksum -= 3600;
		        isWest = true;
	        }

	        float angle = (float)checksum / 10.0f;
	        int cardinal = isWest ? 1 : 0;
	        string name = String.Format("{0}°{1}", angle, isWest ? "W" : "E");
	        return new SatellitePosition(angle, cardinal, name);
        }

        protected bool Equals(SatellitePosition other)
        {
	        return angle.Equals(other.angle) && cardinalDirection == other.cardinalDirection;
        }

        public override bool Equals(object obj)
        {
	        if (ReferenceEquals(null, obj)) return false;
	        if (ReferenceEquals(this, obj)) return true;
	        if (obj.GetType() != this.GetType()) return false;
	        return Equals((SatellitePosition)obj);
        }

        public override int GetHashCode()
        {
	        return HashCode.Combine(angle, cardinalDirection);
        }

    }
}
