import { Injectable } from '@angular/core';
import { IJsonline } from '../models/json-line';
import { ISenMLItem } from '../models/senml-item';
import { IDataLayer } from '../models/data.layer';
import { IItem } from '../models/item';

@Injectable({
  providedIn: 'root',
})
export class SenmlService {
    constructor() {
    }

    getSenMLItem(layer:IDataLayer,jsonLine:IJsonline): ISenMLItem {
        if (jsonLine) {
          const senmlPack = jsonLine.data as ISenMLItem[];
          const temp = senmlPack.filter((i) => i.u == layer.indexKey);
          if (temp.length == 1) return temp[0];
          return null;
        }
    }

    getFirstLayer(item:IItem):IDataLayer {
        if(item && item.data && item.data["layers"] && item.data["layers"].length > 0 ) {
            return item.data["layers"][0] as IDataLayer;
        } else {
            const retVal:IDataLayer = { name:"Soil moisture",index:0,scale:1,unit:"%",indexKey:"%vol" };
            return retVal;
        }
    }
}