import { Injectable } from '@angular/core';

import { CalculatorResult, Range, weightSizes } from 'src/app/core/models';
import { UtilitiesService } from 'src/app/core/services';

@Injectable({ providedIn: 'root' })
export class CalculatorService {
    public coeffKgLbs: number = this._utilitiesService.getCoeffKgLbs();
    public coeffFtM: number = this._utilitiesService.getCoeffFtM();

    constructor(
        private _utilitiesService: UtilitiesService
    ) { }

    private _getTableColumn(jumps: number): number {
        let column: number = 0;
        weightSizes[0].ranges.some((range: Range) => {
            if (jumps >= range.start && jumps <= range.end) {
                column = (range.num-1)
                return true;
            }
            return false;
        });

        return column;
    }

    private _getTableLine(weight: number): number {
        let line: number;
        let min = 60;
        let max = 110;
        if (weight < min) {
            line = min;
        } else if (weight > max) {
            line = max;
        } else {
            line = weight;
        }
        return line;
    }

    private _isInRange(nb: number, range: Range): boolean {
        return (nb >= range.start && nb <= range.end);
    }

    private _reduceLimit(surface: number, percentOff: number): number {
        return Math.ceil(surface * (100 - percentOff) / 100);
    }

    public canopySizeCalc(weight: number, jumps: number): CalculatorResult {
        /* DT48 - 13 mars 2020 */
        /* const maxRange = 2000; */
        /* DT48 - 08 février 2024 */
        const maxRange = 1600;
        let result: CalculatorResult = {
            min: 59,
            min11: 34
        };
        if (jumps > maxRange) {
            return result;
        }
        let index: number = (this._getTableLine(weight) - this._getTableLine(0));
        result.min = weightSizes[index].ranges[this._getTableColumn(jumps)].value;
        result.min11 = this._reduceLimit(result.min, 11);

        return result;
    }

    public convertFeet2Meters(size: number): number {
        return (size * this._utilitiesService.getCoeffFtM());
    }

    public getCanopySizes(weight: number, reduce = false): number[] {
        var data: number[] = [];
        if (weight < 60) {
            weight = 60;
        }
        if (weight > 110) {
            weight = 110;
        }
        if (weight >= 60 && weight <= 110) {
            const line = (weight - this._getTableLine(0));
            let data = weightSizes[line];
            if (reduce) {
                return data.ranges.map((range: Range): number => {
                    return Math.ceil(range.value * (100 - 11) / 100);
                });
            } else {
                return data.ranges.map((range: Range): number => {
                    return range.value;
                });
            }
        }
        return data;
    }

    public getCharge(canopySize: number, nakedWeight: number, equipementWeight: number): number {
        return (((nakedWeight + equipementWeight) * this.coeffKgLbs) / canopySize);
    }

    public getRangeNum(jumps: number): number {
        let data: Range[] = weightSizes[0].ranges;
        let num: number = 1;
        /* DT48 - 13 mars 2020 */
        /* const maxRange = 2000; */
        /* DT48 - 08 février 2024 */
        const maxRange = 1600;
        data.some((range: Range): boolean => {
            if (jumps >= range.start && jumps <= range.end) {
                num = range.num;
                return true;
            } else if (jumps > maxRange) {
                num = 9;
                return true;
            }
            return false;
        });
        return num;
    }

    public getStateColor(values: CalculatorResult): string {
        let color: string = 'danger';
        if (values.current! >= values.min11 && values.current! < values.min) {
            color = 'warning';
        }
        if (values.current! >= values.min) {
            color = 'success';
        }

        return color;
    }

}