/*
 * Copyright 2013 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.cmatic;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Stream;

import br.com.objectos.core.Strings;
import br.com.objectos.pojo.Pojo;

/**
 * @author edenir.anschau@objectos.com.br (Edenir Norberto Anschau)
 */
@Pojo
public abstract class Lancamentos {

  abstract String empresa();
  abstract LocalDate ano();
  abstract List<IsLancamento> lancamentos();

  private String nome;
  private String log;
  private String txt;

  Lancamentos() {
  }

  public static LancamentosBuilder builder() {
    return new LancamentosBuilderPojo();
  }

  public String getNome() {
    if (nome == null) {
      String nome = Strings
          .accentsToAscii(empresa())
          .stripWhitespace()
          .toString()
          .toUpperCase();
      String data = LocalDateFormat.YY.format(ano());
      this.nome = String.format("%s.M%s", nome, data);
    }
    return nome;
  }

  public String toLog() {
    if (log == null) {
      Stream<String> lines = lancamentos().stream()
          .flatMap(input -> input.toLog().stream());
      log = WayCMatic.joinLines(lines);
    }
    return log;
  }

  public String toTxt() {
    if (txt == null) {
      Stream<String> lines = lancamentos().stream()
          .map(IsLancamento::toTxt);
      txt = WayCMatic.joinLines(lines);
    }
    return txt;
  }

}