package com.jonasgerdes.schauburgr.network.guide_converter;

import com.jonasgerdes.schauburgr.model.Guide;
import com.jonasgerdes.schauburgr.model.Movie;
import com.jonasgerdes.schauburgr.model.Screening;

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by jonas on 04.03.2017.
 * Website of Schauburg Cineworld is kinda weird, so we have to parse JavaScript here :(
 */

public class SchauburgGuideConverter implements Converter<ResponseBody, Guide> {

    public static final class Factory extends Converter.Factory {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type,
                                                                Annotation[] annotations,
                                                                Retrofit retrofit) {
            return INSTANCE;
        }

    }

    private static final SchauburgGuideConverter INSTANCE = new SchauburgGuideConverter();

    private final MovieParser mMovieParser;
    private final ScreeningParser mScreeningParser;

    private SchauburgGuideConverter() {
        mMovieParser = new MovieParser();
        mScreeningParser = new ScreeningParser();
    }

    @Override
    public Guide convert(ResponseBody value) throws IOException {
        Guide guide = new Guide();
        BufferedReader reader = new BufferedReader(value.charStream());

        String line = reader.readLine();
        Movie movie;
        Screening screening;
        while (line != null) {
            //try screen parsing first increases performance
            //since there are more screenings then movies
            screening = mScreeningParser.parse(line, guide.getMovies());
            if (screening != null) {
                guide.addScreening(screening);
            } else {
                movie = mMovieParser.parse(line);
                if (movie != null) {
                    guide.addMovie(movie);
                }
            }
            line = reader.readLine();
        }

        return guide;
    }

}

@@ -255,24 +255,58 @@
 OException;%0A
+import java.io.InputStreamReader;%0A
 import java.
@@ -363,16 +363,49 @@
 ct.Type;
+%0Aimport java.nio.charset.Charset;
 %0A%0Aimport
@@ -696,16 +696,93 @@
 Guide%3E %7B
+%0A    private static final Charset ISO_8859_1 = Charset.forName(%22ISO-8859-1%22);
 %0A%0A    pu
@@ -1655,26 +1655,65 @@
 der(
+%0A                new InputStreamReader(
 value.
-char
+byte
 Stream()
 );%0A%0A
@@ -1708,16 +1708,38 @@
 Stream()
+, ISO_8859_1)%0A        
 );%0A%0A    

Fix encoding issue