/***********************************************************************
Use            : To declare the application reusable static methods
Created By     : ATP-0000835
Created Date   : 14-FEB-2017
Updated By     : ATP-0000835
Updated Date   : 14-FEB-2017
***********************************************************************/

import {  Response } from '@angular/http';
import { Observable }     from 'rxjs/Observable';

export class AppUtility {
        
      // Get recods from the respoce body
      public static  extractDataFromResponce(res: Response) {
        return res.json();        
      }
        
      //Handle error from server responce
      public static handleHttpErrors (error: any) {        
        let errMsg = (error.message) ? error.message :
          error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        this.LogText(errMsg, 3);        
        return Observable.throw(errMsg);
      }  
      
      //Format a string with the provided arguments
      public static formatString(text: string, appenders: any[]) {
           if (appenders.length == 0) {
                return text;
            }

            var str = text;
            for (var i = 0; i < appenders.length; i++) {
                var placeHolder = '{' + (i) + '}';
                str = str.replace(placeHolder, appenders[i]);
            }

            return str;
      }
      
      //Log text messages in browser console
      public static LogText(content: any, type?: number) {        
         if(!type || type === 1) {
           console.log('%c Metada Editor: ', 'background: #000; color: #FFF', content + ' / ' + new Date());       
         } else if(type === 2) {
           console.warn('%c Metada Editor: ', 'background: #000; color: #FFF', content + ' / ' + new Date());       
         } else if(type === 3) {
           console.error('%c Metada Editor: ', 'background: #000; color: #FFF', content + ' / ' + new Date());       
         }
      }
}