import { FeedCard } from '../models/feed-card.model';
import { FeedBook } from '../models/feed-book.model';
import { FeedEvent } from '../models/feed-event.model';
import { FeedNews } from '../models/feed-news.model';
import { FeedRecommandation } from '../models/feed-recommandation.model';
import { Injectable } from '@angular/core';
import { FeedKpi } from '../models/feed-kpi.model';

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

  fromFeedDTO(feed: any): FeedCard {
    const books: FeedBook[] = [];
    const event: FeedEvent[] = [];
    const news: FeedNews[] = [];
    const recommandation: FeedRecommandation = null;
    let kpi: FeedKpi = null;

    if (feed.items && feed.type === 'products') {
      feed.items.forEach((item) => {
        books.push({
          ean: item.product.ean,
          name: item.product.name,
        });
      });
    } else if (feed.type === 'info') {
      feed.items.forEach((i) => {
        news.push(<FeedNews>{
          id: i.id,
          title: i.heading,
          content: i.text,
          icon: i.image,
        });
      });
    } else if (feed.type === 'kpi') {
      kpi = {
        actual: feed.items[0].actual,
        target: feed.items[0].target,
        percantage: Math.round((feed.items[0].actual / feed.items[0].target) * 100),
      };
    }

    return <FeedCard>{
      cardTitle: feed.label,
      type: feed.type,
      headline: feed.headline ? feed.headline : '',
      text: feed.desc ? feed.desc : '',
      books: books,
      event: event,
      news: news,
      kpi: kpi,
      recommandation: recommandation,
      showRecommIcon: feed.emphasize ? true : false,
    };
  }
}
