/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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 androidx.media3.extractor.text.ttml;

import static android.graphics.Color.BLACK;
import static android.graphics.Color.RED;
import static android.graphics.Color.YELLOW;
import static androidx.media3.extractor.text.ttml.TtmlRenderUtil.resolveStyle;
import static androidx.media3.extractor.text.ttml.TtmlStyle.STYLE_BOLD;
import static androidx.media3.extractor.text.ttml.TtmlStyle.STYLE_BOLD_ITALIC;
import static com.google.common.truth.Truth.assertThat;

import android.graphics.Color;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Unit test for {@link TtmlRenderUtil}. */
@RunWith(AndroidJUnit4.class)
public final class TtmlRenderUtilTest {

  @Test
  public void resolveStyleNoStyleAtAll() {
    assertThat(resolveStyle(null, null, null)).isNull();
  }

  @Test
  public void resolveStyleSingleReferentialStyle() {
    Map<String, TtmlStyle> globalStyles = getGlobalStyles();
    String[] styleIds = {"s0"};

    assertThat(TtmlRenderUtil.resolveStyle(null, styleIds, globalStyles))
        .isSameInstanceAs(globalStyles.get("s0"));
  }

  @Test
  public void resolveStyleMultipleReferentialStyles() {
    Map<String, TtmlStyle> globalStyles = getGlobalStyles();
    String[] styleIds = {"s0", "s1"};

    TtmlStyle resolved = TtmlRenderUtil.resolveStyle(null, styleIds, globalStyles);
    assertThat(resolved).isNotSameInstanceAs(globalStyles.get("s0"));
    assertThat(resolved).isNotSameInstanceAs(globalStyles.get("s1"));
    assertThat(resolved.getId()).isNull();

    // inherited from s0
    assertThat(resolved.getBackgroundColor()).isEqualTo(BLACK);
    // inherited from s1
    assertThat(resolved.getFontColor()).isEqualTo(RED);
    // merged from s0 and s1
    assertThat(resolved.getStyle()).isEqualTo(STYLE_BOLD_ITALIC);
  }

  @Test
  public void resolveMergeSingleReferentialStyleIntoInlineStyle() {
    Map<String, TtmlStyle> globalStyles = getGlobalStyles();
    String[] styleIds = {"s0"};
    TtmlStyle style = new TtmlStyle();
    style.setBackgroundColor(Color.YELLOW);

    TtmlStyle resolved = TtmlRenderUtil.resolveStyle(style, styleIds, globalStyles);
    assertThat(resolved).isSameInstanceAs(style);

    // inline attribute not overridden
    assertThat(resolved.getBackgroundColor()).isEqualTo(YELLOW);
    // inherited from referential style
    assertThat(resolved.getStyle()).isEqualTo(STYLE_BOLD);
  }

  @Test
  public void resolveMergeMultipleReferentialStylesIntoInlineStyle() {
    Map<String, TtmlStyle> globalStyles = getGlobalStyles();
    String[] styleIds = {"s0", "s1"};
    TtmlStyle style = new TtmlStyle();
    style.setBackgroundColor(Color.YELLOW);

    TtmlStyle resolved = TtmlRenderUtil.resolveStyle(style, styleIds, globalStyles);
    assertThat(resolved).isSameInstanceAs(style);

    // inline attribute not overridden
    assertThat(resolved.getBackgroundColor()).isEqualTo(YELLOW);
    // inherited from both referential style
    assertThat(resolved.getStyle()).isEqualTo(STYLE_BOLD_ITALIC);
  }

  @Test
  public void resolveStyleOnlyInlineStyle() {
    TtmlStyle inlineStyle = new TtmlStyle();
    assertThat(TtmlRenderUtil.resolveStyle(inlineStyle, null, null)).isSameInstanceAs(inlineStyle);
  }

  private static Map<String, TtmlStyle> getGlobalStyles() {
    Map<String, TtmlStyle> globalStyles = new HashMap<>();

    TtmlStyle s0 = new TtmlStyle();
    s0.setId("s0");
    s0.setBackgroundColor(Color.BLACK);
    s0.setBold(true);
    globalStyles.put(s0.getId(), s0);

    TtmlStyle s1 = new TtmlStyle();
    s1.setId("s1");
    s1.setBackgroundColor(Color.RED);
    s1.setFontColor(Color.RED);
    s1.setItalic(true);
    globalStyles.put(s1.getId(), s1);

    return globalStyles;
  }
}
