//
// Copyright 2021–2022 alterNERDtive.
//
// This file is part of EDNA.
//
// EDNA is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EDNA is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EDNA. If not, see <https://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace alterNERDtive.Edna
{
///
/// A CMDR in the galaxy of Elite Dangerous.
///
public class Commander : Locatable
{
/// Initializes a new instance of the class.
///
/// The CMDR’s name.
/// The CMDR’s EDSM API key.
private Commander(string name, string? edsmProfileUrl, DateTime? lastActiveAt, StarSystem? starsystem, Coordinates? coordinates)
=> (this.Name, this.EdsmProfileUrl, this.LastActiveAt, this.StarSystem, this.Coordinates) = (name, edsmProfileUrl, lastActiveAt, starsystem, coordinates);
///
/// Gets the CMDR’s name.
///
public string Name { get; }
///
/// Gets the CMDR’s EDSM profile URL.
///
public string? EdsmProfileUrl { get; }
///
/// Gets the CMDR’s date of last activity.
///
public DateTime? LastActiveAt { get; }
///
/// Gets the CMDR’s current star system.
///
public StarSystem? StarSystem { get; }
///
/// Gets the CMDR’s current coordinates.
///
public new Coordinates? Coordinates { get; }
///
/// Finds a CMDR by name. Optionally takes an EDSM API key to access a
/// private profile.
///
/// The CMDR’s name.
/// The CMDR’s EDSM API key.
/// The CMDR.
public static Commander Find(string name, string? apiKey = null)
{
return FindAsync(name, apiKey).GetAwaiter().GetResult();
}
///
///
///
///
///
///
public static async Task FindAsync(string name, string? apiKey = null)
{
if (apiKey != null)
{
throw new NotImplementedException();
}
try
{
Edsm.ApiCmdr cmdr = await Edsm.LogsApi.FindCmdr(name);
return new Commander(name, cmdr.Url, DateTime.Parse(cmdr.DateLastActivity), StarSystem.Find(cmdr.SystemId64!.Value), new Coordinates(cmdr.Coordinates!.Value));
}
catch (ArgumentException e)
{
throw new CommanderNotFoundException(e.Message, e);
}
catch (AccessViolationException e)
{
throw new CommanderHiddenException(e.Message, e);
}
}
}
}