/*
 * Copyright © 2016 Cask Data, Inc.
 *
 * 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 io.cdap.cdap.logging.gateway.handlers;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import io.cdap.cdap.logging.read.LogOffset;

/**
 * Formatted LogOffset
 */
public class FormattedLogOffset {

  private static final char SEPARATOR = '.';

  @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
  private final String offset;

  public FormattedLogOffset(LogOffset logOffset) {
    this.offset = formatLogOffset(logOffset);
  }

  public static String formatLogOffset(LogOffset logOffset) {
    return Joiner.on(SEPARATOR).join(logOffset.getKafkaOffset(), logOffset.getTime());
  }

  public static LogOffset parseLogOffset(String offsetStr) {
    if (offsetStr.isEmpty()) {
      return LogOffset.LATEST_OFFSET;
    }

    Iterable<String> splits = Splitter.on(SEPARATOR).split(offsetStr);
    Preconditions.checkArgument(Iterables.size(splits) == 2, "Invalid offset provided: %s",
        offsetStr);

    return new LogOffset(Long.valueOf(Iterables.get(splits, 0)),
        Long.valueOf(Iterables.get(splits, 1)));
  }
}
