
import { AxiosResponse, AxiosInstance, AxiosError } from "axios";
import { inject } from 'vue'
import { WorkoutItem, ChartData } from "../Definitions.vue";

let workoutItem: WorkoutItem;
let mapCenter: Array<number> = ([37.617644, 55.755819]);
let lineCoordinates: Array<Array<number>> = [];
let data: ChartData;
let dzenLink: string;

const msToKmh = (ms: number) => ms * 3.6;
export type InitWorkoutItem = {
    workoutItem: WorkoutItem;
    mapCenter: Array<number>;
    lineCoordinates: Array<Array<number>>;
    data: ChartData;
    dzenLink?: string;

}

export const GetWorkout = (url: string)  => {
    const axiosAuth = inject('axiosAuth') as AxiosInstance;
    const query = axiosAuth
        .get(url)
        .then((response: AxiosResponse) => {
            workoutItem = response.data.workout;
            let times = [];
            let speed = [];
            let heart_rate = [];
            let power = [];
            let coords = [];
            let elevation = [];
            for (let i in response.data.results) {
                times.push(response.data.results[i].timestamp);
                coords.push([response.data.results[i].longitude, response.data.results[i].latitude]);
                if (response.data.results[i].elevation) {
                    elevation.push(response.data.results[i].elevation);
                }
                if (response.data.results[i].speed) {
                    speed.push(msToKmh(response.data.results[i].speed));
                }
                if (response.data.results[i].power) {
                    power.push(response.data.results[i].power);
                }
                if (response.data.results[i].heart_rate) {
                    heart_rate.push(response.data.results[i].heart_rate);
                }
            }
            lineCoordinates = coords
            mapCenter = [coords[0][0], coords[0][1]];
            let datasets = [];
            if (speed.length > 0) {
                datasets.push({ radius: 0, label: 'Скорость', borderColor: '#00aa00', backgroundColor: '#00aa00', data: speed });
            }
            if (heart_rate.length > 0) {
                datasets.push({ radius: 0, label: 'Пульс', borderColor: '#990000', backgroundColor: '#990000', data: heart_rate, });
            }
            if (power.length > 0) {
                datasets.push({ radius: 0, label: 'Мощность', borderColor: '#cccccc', backgroundColor: '#cccccc', data: power, });
            }
            if (elevation.length > 0) {
                datasets.push( { radius: 0, label: 'Подъем', borderColor: '#000', backgroundColor: '#000', data: elevation, });
            }
            data = {
                labels: times,
                datasets: datasets

            }
            if (response.data.workout.external_links && response.data.workout.external_links.values && response.data.workout.external_links.values.length > 0) {
                dzenLink = response.data.workout.external_links.values[0].value;
            }
            return { workoutItem, mapCenter, lineCoordinates, data, dzenLink };
        });
        return Promise.resolve(query);
};