package org.lodder.subtools.multisubdownloader.lib.control.subtitles.sorting;

import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.Subtitle;


import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class ScoreCalculatorTest {

  protected ScoreCalculator calculator;

  @Before
  public void setUp() throws Exception {
    calculator = null;
  }

  @Test
  public void test_it_calculates_the_score_for_subtitle() throws Exception {
    SortWeight weights = createWeights();

    calculator = new ScoreCalculator(weights);
    int score;
    Subtitle subtitle;

    subtitle =
      createSubtitle("Arrested.Development.S01E01.DVDRip.XviD-MEDiEVAL.srt", "dvdrip xvid", "medieval");
    score = calculator.calculate(subtitle);
    assertEquals(convertToPercentage(8, weights), score);

    subtitle =
      createSubtitle("Arrested Development 1x1 works with medieval.srt", "", "medieval");
    score = calculator.calculate(subtitle);
    assertEquals(convertToPercentage(5, weights), score);

    subtitle =
      createSubtitle("Arrested.Development.S01E01.BDrip.XviD-Acme.srt", "", "acme");
    score = calculator.calculate(subtitle);
    assertEquals(convertToPercentage(1, weights), score);

    subtitle =
      createSubtitle("Arrested.Development.S01E01.DVDRip.DivX-Acme.srt", "", "acme");
    score = calculator.calculate(subtitle);
    assertEquals(convertToPercentage(2, weights), score);

    subtitle =
      createSubtitle("Arrested.Development.S01E01.BluRay.x264-OSCORP.srt", "", "oscorp");
    score = calculator.calculate(subtitle);
    assertEquals(convertToPercentage(0, weights), score);
  }

  private int convertToPercentage(int score, SortWeight weights){
    return (score / weights.getMaxScore()) * 100;
  }

  private Subtitle createSubtitle(String filename, String quality, String team) {
    Subtitle subtitle = mock(Subtitle.class);
    when(subtitle.getFilename()).thenReturn(filename);
    when(subtitle.getQuality()).thenReturn(quality);
    when(subtitle.getTeam()).thenReturn(team);
    return subtitle;
  }

  private SortWeight createWeights() {
    // Arrested.Development.S01E01.DVDRip.XviD-MEDiEVAL
    Release release = mock(Release.class);
    when(release.getQuality()).thenReturn("DVDRip XviD");
    when(release.getReleasegroup()).thenReturn("MEDiEVAL");

    HashMap<String, Integer> definedWeights = new HashMap<>();
    definedWeights.put("dvdrip", 2);
    definedWeights.put("xvid", 1);
    definedWeights.put("%GROUP%", 5);

    return new SortWeight(release, definedWeights);
  }
}

@@ -607,16 +607,41 @@
 Weights(
+%22DVDRip XviD%22, %22MEDiEVAL%22
 );%0A%0A    
@@ -907,39 +907,11 @@
 als(
-convertToPercentage(8, weights)
+100
 , sc
@@ -1087,39 +1087,10 @@
 als(
-convertToPercentage(5, weights)
+63
 , sc
@@ -1261,39 +1261,10 @@
 als(
-convertToPercentage(1, weights)
+13
 , sc
@@ -1436,39 +1436,10 @@
 als(
-convertToPercentage(2, weights)
+25
 , sc
@@ -1615,168 +1615,410 @@
 als(
-convertToPercentage(0, weights), score);%0A  %7D%0A%0A  private int convertToPercentage(int score, SortWeight weights)%7B%0A    return (score / weights.getMaxScore()) * 100
+0, score);%0A%0A    /* test bugfix: #27 */%0A    weights = createWeights(%22720p hdtv x264%22, %22DIMENSION%22);%0A    calculator = new ScoreCalculator(weights);%0A%0A    subtitle =%0A      createSubtitle(%22Criminal Minds - 10x12 - Anonymous 720pDimension Vertaling: Het Criminal Minds Team%22, %22720p%22, %22720pDimension Vertaling: Het Criminal Minds Team%22);%0A    score = calculator.calculate(subtitle);%0A    assertEquals(70, score)
 ;%0A  
@@ -2364,16 +2364,44 @@
 Weights(
+String quality, String group
 ) %7B%0A    
@@ -2537,29 +2537,23 @@
 nReturn(
-%22DVDRip XviD%22
+quality
 );%0A    w
@@ -2594,26 +2594,21 @@
 nReturn(
-%22MEDiEVAL%22
+group
 );%0A%0A    
@@ -2666,16 +2666,51 @@
 ap%3C%3E();%0A
+    definedWeights.put(%22hdtv%22, 2);%0A
     defi
@@ -2762,20 +2762,90 @@
 ts.put(%22
-xvid
+720p%22, 2);%0A    definedWeights.put(%22xvid%22, 1);%0A    definedWeights.put(%22x264
 %22, 1);%0A 

Edit ScoreCalculatorTest