using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using ActiproSoftware.Windows.Extensions;
using JD.Shotgun.Service;
using JD.Shotgun.Service.ShotgunEntities;
using JD.Shotgun.WPF.Settings;
using Plume.Interfaces.Settings;
using Action = Plume.Interfaces.WPF.Actions.Action;
namespace JD.Shotgun.WPF.Views
{
///
/// Interaction logic for MapExplorerView.xaml
///
public partial class ShotgunMapExplorerView : UserControl
{
private readonly ShotgunSettings _settings;
private readonly IShotgunService _shotgunService;
private readonly Func> _getActionsFunc;
public event EventHandler MapSelected;
public event EventHandler OpusSelected;
public ShotgunMapExplorerView(IShotgunService shotgunService, ISettingsManager settingsManager, Func> getActionsFunc)
{
InitializeComponent();
Actions = new ObservableCollection();
_shotgunService = shotgunService;
_settings = settingsManager.Get();
_getActionsFunc = getActionsFunc;
UpdateOpuses();
}
private void UpdateOpuses()
{
ComboBoxOpus.Items.Clear();
var opuses = _shotgunService.GetAllOpuses().Reverse().ToList();
foreach (var shotgunJdOpus in opuses)
{
ComboBoxOpus.Items.Add(shotgunJdOpus);
}
ComboBoxOpus.SelectedIndex = !string.IsNullOrEmpty(_settings.CurrentOpus) ? Math.Max(0, opuses.FindIndex(opus => opus.Name == _settings.CurrentOpus)) : 0;
}
private void ComboBoxOpus_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxMaps.Items.Clear();
if (e.AddedItems.Count > 0 && e.AddedItems[0] is ShotgunOpus opus)
{
_settings.CurrentOpus = opus.Name;
var maps = _shotgunService.GetOpusMaps(opus).OrderBy(x => x.Name).ToList();
foreach (var shotgunJdMap in maps)
{
ComboBoxMaps.Items.Add(shotgunJdMap);
}
ComboBoxMaps.SelectedIndex = !string.IsNullOrEmpty(_settings.CurrentMap) ? Math.Max(0, maps.FindIndex(x => x.Name == _settings.CurrentMap)) : 0;
OpusSelected?.Invoke(this, opus);
}
else
{
ComboBoxMaps.SelectedIndex = 0;
OpusSelected?.Invoke(this, null);
}
}
private void ComboBoxMaps_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && e.AddedItems[0] is ShotgunMap map)
{
_settings.CurrentMap = map.Name;
Thumbnail.Source = string.IsNullOrEmpty(map.ThumbnailUrl) ?
new BitmapImage(new Uri("/JD.Shotgun.WPF;component/Resources/thumbnail-default.png", UriKind.Relative)) :
new BitmapImage(new Uri(map.ThumbnailUrl));
MapSelected?.Invoke(this, map);
}
else
{
MapSelected?.Invoke(this, null);
}
RefreshActions();
}
private void RefreshActions()
{
Actions.Clear();
if (ComboBoxMaps.SelectedValue is ShotgunMap map)
{
Actions.AddRange(_getActionsFunc(map));
}
}
public void Select(ShotgunMapAsset asset)
{
ComboBoxOpus.SelectedIndex = ComboBoxOpus.Items.Cast().ToList().FindIndex(opus => opus.Name == asset.MapOpusName);
ComboBoxMaps.SelectedIndex = ComboBoxMaps.Items.Cast().ToList().FindIndex(map => map.Name == asset.MapName);
}
public static readonly DependencyProperty ActionsProperty = DependencyProperty.Register("Actions", typeof(ObservableCollection), typeof(ShotgunMapExplorerView), new PropertyMetadata(default(ObservableCollection)));
public ObservableCollection Actions
{
get { return (ObservableCollection)GetValue(ActionsProperty); }
set { SetValue(ActionsProperty, value); }
}
}
}