using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using BreCalClient.misc.Api; using BreCalClient.misc.Client; using BreCalClient.misc.Model; namespace BreCalClient { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { DefaultApi _api; ObservableCollection _controlModels = new(); List _ships = new(); Dictionary _shipLookupDict = new(); List _berths = new(); Dictionary _berthLookupDict = new(); List _participants = new(); Dictionary _participantLookupDict = new(); public MainWindow() { InitializeComponent(); _api = new DefaultApi(); _api.Configuration.ApiKeyPrefix["Authorization"] = "Bearer"; } #region event handler private void Window_Loaded(object sender, RoutedEventArgs e) { } private async void buttonLogin_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(this.textPassword.Password) || string.IsNullOrEmpty(this.textUsername.Text)) { this.labelLoginResult.Content = Application.Current.FindResource("textUserNamePasswordEmpty").ToString(); return; } Credentials credentials = new(username: textUsername.Text.Trim(), password: textPassword.Password.Trim()); try { LoginResult loginResult = await _api.LoginPostAsync(credentials); if(loginResult != null) { if(loginResult.Id > 0) { this.busyIndicator.IsBusy = false; this._api.Configuration.ApiKey["Authorization"] = loginResult.Token; this.LoadStaticLists(); } } } catch (ApiException ex) { this.labelLoginResult.Content = ex.Message; } catch(Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } private void buttonExit_Click(object sender, RoutedEventArgs e) { this.Close(); } #endregion #region private methods private async void LoadStaticLists() { this._berths = await _api.BerthsGetAsync(); foreach(var berth in this._berths) _berthLookupDict[berth.Id] = berth; this._ships = await _api.ShipsGetAsync(); foreach(var ship in this._ships) _shipLookupDict[ship.Id] = ship; // this._participants = await _api.ParticipantGetAsync(); } private async void RefreshShipcalls() { List shipcalls = await _api.ShipcallsGetAsync(0); // TODO: this must return all current calls foreach(Shipcall shipcall in shipcalls) { // TODO: find out if we already have this thing // yes: update values // no: create new entry ShipcallControlModel scm = new ShipcallControlModel(); scm.Shipcall = shipcall; if(this._shipLookupDict.ContainsKey(shipcall.Id)) scm.Ship = this._shipLookupDict[shipcall.Id]; _controlModels.Add(scm); // TODO: sort this list somehow } } #endregion } }