using System;
using System.Collections.Generic;

namespace TIASshot {
    /// <summary>
    /// ImagingSource カメラの接続先デバイスの選択（IScam から切り出した判断ロジック．SDK に依存しない）．
    /// IScam の DeviceName は IC Imaging Control の列挙名（例: "DFK 33UX178"）から空白を除いたキー（例: "DFK33UX178"）で，
    /// デバイス状態ファイル名（{DeviceName}.xml）と config.xml の機種別設定のキーにも使う．
    /// </summary>
    internal static class IScamDeviceSelector {

        /// <summary>
        /// 列挙名から DeviceName（デバイス状態ファイル名・設定キー）に使うキーを作る（空白を除く）．
        /// </summary>
        /// <param name="deviceName">IC Imaging Control の列挙名（Device.Name）</param>
        /// <returns>空白を除いた名前．null のときは null</returns>
        internal static string ToDeviceKey(string deviceName) {
            return deviceName?.Replace(" ", "");
        }

        /// <summary>
        /// 列挙されたデバイス名の中から，キーが deviceKey に一致する最初の名前を選ぶ（大文字小文字を区別する）．
        /// 返した名前を ICImagingControl.Device に設定して開く．
        /// </summary>
        /// <param name="deviceNames">IC Imaging Control が列挙したデバイス名（Devices[].Name）．null 可</param>
        /// <param name="deviceKey">探す DeviceName（空白を除いたキー）</param>
        /// <returns>開くデバイスの列挙名．見つからなければ null</returns>
        internal static string FindDeviceName(IEnumerable<string> deviceNames, string deviceKey) {
            if (deviceNames == null || string.IsNullOrEmpty(deviceKey)) return null;
            foreach (var name in deviceNames) {
                if (string.IsNullOrEmpty(name)) continue;
                if (string.Equals(ToDeviceKey(name), deviceKey, StringComparison.Ordinal)) return name;
            }
            return null;
        }
    }
}
