/*
 * MIT License
 * 
 * Copyright (c) Robin E. R. Davies
 * Copyright (c) Gabriel Hernandez
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */


export default class AlsaDeviceInfo {
    deserialize(input: any): AlsaDeviceInfo {
        this.cardId = input.cardId;
        this.id = input.id;
        this.name = input.name;
        this.longName = input.longName;
        this.sampleRates = input.sampleRates as number[];
        this.minBufferSize = input.minBufferSize;
        this.maxBufferSize = input.maxBufferSize;
        this.supportsCapture = input.supportsCapture ? true : false;
        this.supportsPlayback = input.supportsPlayback ? true : false;
        this.captureBusy = input.captureBusy;
        this.playbackBusy = input.playbackBusy;
        return this;
    }
    static deserialize_array(input: any): AlsaDeviceInfo[]
    {
        let result: AlsaDeviceInfo[] = [];
        for (let i = 0; i < input.length; ++i)
        {
            result.push(new AlsaDeviceInfo().deserialize(input[i]));
        }
        return result;
    }
    closestSampleRate(sr: number): number
    {
        let result = 48000;
        let bestError = 1E36;
        for (let rate of this.sampleRates)
        {
            let error = (sr-rate)*(sr-rate);
            if (error < bestError)
            {
                bestError = error;
                result = rate;
            }

        }
        return result;
    }
    closestBufferSize(buffSize: number): number
    {
        let result = 64;
        let bestError = 1E36;

        for (let sz = 2; sz <= this.maxBufferSize; sz *= 2)
        {
            if (sz >= this.minBufferSize)
            {
                let error = (sz-buffSize)*(sz-buffSize);
                if (error < bestError)
                {
                    bestError = error;
                    result = sz;
                }
            }
        }
        return result;
    }

    cardId: number = -1;
    id: string = "";
    name: string = "";
    longName: string = "";
    sampleRates: number[] = [];
    minBufferSize: number = 0;
    maxBufferSize: number = 0; 
    supportsCapture: boolean = true;
    supportsPlayback: boolean = true;
    captureBusy: boolean = false;
    playbackBusy: boolean = false;
};