package fibonacci;

import tester.Tester;

class Examples {

    ISequence<Integer> f;
    ISequence<String> a;
    ISequence<String> m;
    ISeqGen<String> aGen = new AGen();

    ProbTab tab = new ProbTab(
        new Cons<WordProb>(
            new WordProb(
                "never",
                new Cons<ProbPair>(new ProbPair("gonna", 3), new Mt<ProbPair>())
            ),
            new Cons<WordProb>(
                new WordProb(
                    "gonna",
                    new Cons<ProbPair>(
                        new ProbPair("give", 3),
                        new Mt<ProbPair>()
                    )
                ),
                new Cons<WordProb>(
                    new WordProb(
                        "give",
                        new Cons<ProbPair>(
                            new ProbPair("you", 3),
                            new Mt<ProbPair>()
                        )
                    ),
                    new Mt<WordProb>()
                )
            )
        )
    );
    ISeqGen<String> markov = new Markov(tab, "never");

    void init() {
        this.f = new Fibonacci();
        this.a = new GenSeq<String>("", aGen);
        this.m = new GenSeq<String>("", markov);
    }

    void testFibonacci(Tester t) {
        init();
        t.checkExpect(f.get(), 0);
        t.checkExpect(f.get(), 1);
        t.checkExpect(f.get(), 1);
        t.checkExpect(f.get(), 2);
        t.checkExpect(f.get(), 3);
        t.checkExpect(f.get(), 5);
        t.checkExpect(f.get(), 8);
    }

    void testFibonacciNth(Tester t) {
        t.checkExpect(Fibonacci.nth(0), 0);
        t.checkExpect(Fibonacci.nth(1), 1);
        t.checkExpect(Fibonacci.nth(2), 1);
        t.checkExpect(Fibonacci.nth(3), 2);
        t.checkExpect(Fibonacci.nth(4), 3);
        t.checkExpect(Fibonacci.nth(5), 5);
        t.checkExpect(Fibonacci.nth(6), 8);
    }

    void testASeq(Tester t) {
        init();
        t.checkExpect(a.get(), "");
        t.checkExpect(a.get(), "a");
        t.checkExpect(a.get(), "aa");
        t.checkExpect(a.get(), "aaa");
        t.checkExpect(a.get(), "aaaa");
        t.checkExpect(a.get(), "aaaaa");
    }

    void testMarkov(Tester t) {
        init();
        t.checkExpect(m.get(), "");
        t.checkExpect(m.get(), "never");
        t.checkExpect(m.get(), "never gonna");
        t.checkExpect(m.get(), "never gonna give");
    }
}

// A generic list.
interface ILo<A> {
    // Find the element that matches the string.
    A findMatch(GenMatch<A> match, String a);
    // Find the maximum of the list.
    A findMax(IntVal<A> intVal);
    A findMaxHelper(IntVal<A> intVal, A biggestSoFar, int biggestSoFarVal);
}

class Cons<A> implements ILo<A> {

    A first;
    ILo<A> rest;

    Cons(A first, ILo<A> rest) {
        this.first = first;
        this.rest = rest;
    }

    public A findMatch(GenMatch<A> match, String s) {
        if (match.match(s, this.first)) return this.first;
        else return this.rest.findMatch(match, s);
    }

    public A findMax(IntVal<A> intVal) {
        return this.findMaxHelper(intVal, this.first, intVal.apply(this.first));
    }

    public A findMaxHelper(
        IntVal<A> intVal,
        A biggestSoFar,
        int biggestSoFarVal
    ) {
        int thisVal = intVal.apply(this.first);
        if (thisVal > biggestSoFarVal) return this.rest.findMaxHelper(
                intVal,
                this.first,
                thisVal
            );
        else return this.rest.findMaxHelper(
                intVal,
                biggestSoFar,
                biggestSoFarVal
            );
    }
}

class Mt<A> implements ILo<A> {

    public A findMatch(GenMatch<A> match, String s) {
        throw new Error("Can't find a match: " + s);
    }

    public A findMax(IntVal<A> intVal) {
        throw new Error("Can't find max of empty list.");
    }

    public A findMaxHelper(
        IntVal<A> intVal,
        A biggestSoFar,
        int biggestSoFarVal
    ) {
        return biggestSoFar;
    }
}

interface ISequence<X> {
    // Returns the current element in the sequence.
    // EFFECT: Updates the state to the next element in the sequence.
    X get();
}

// Sequence generator method.
interface ISeqGen<X> {
    X gen(X state); // Generates the next value in the sequence given the current state.
}

// The Fibonacci sequence.
class Fibonacci implements ISequence<Integer> {

    int idx; // Number of times `get()` has been called.
    int pre; // The previous (last) number.
    int pen; // The penultimate (second to last) number.

    Fibonacci() {
        this.pen = 0;
        this.pre = 1;
    }

    // Get the next in the sequence.
    public Integer get() {
        int ret; // What to return.
        /*
         *
         * idx pen pre ret 0 0 1 0 1 0 1 1 2 1 1 2 3 1 2 3 4 2 3 5 5 3 5 8
         *
         */

        // F(0) = 0; F(1) = 1
        if (this.idx <= 1) ret = this.idx;
        else { // F(n) = F(n-1) + F(n-2)
            ret = this.pre + this.pen;

            this.pen = this.pre;
            this.pre = ret;
        }

        this.idx = this.idx + 1;
        return ret;
    }

    static Integer nth(int n) {
        return n <= 1 ? n : nth(n - 1) + nth(n - 2);
    }
}

// A generic sequence.
class GenSeq<X> implements ISequence<X> {

    X state; // The current state of the sequence.
    ISeqGen<X> seqGen; // The method by which new values are generated.

    // Create new generic sequence given an initial state.
    GenSeq(X init, ISeqGen<X> seqGen) {
        this.state = init;
        this.seqGen = seqGen;
    }

    public X get() {
        X ret = this.state;
        this.state = this.seqGen.gen(this.state);
        return ret;
    }
}

// A string of "a"s.
class AGen implements ISeqGen<String> {

    public String gen(String state) {
        return state.concat("a");
    }
}

// A simple markov chain text generator, with a working memory of 1 word.
class Markov implements ISeqGen<String> {

    ProbTab tab;
    String initState;

    Markov(ProbTab tab, String initState) {
        this.tab = tab;
        this.initState = initState;
    }

    public String gen(String state) {
        if (state.isEmpty()) return initState;
        return state
            .concat(" ")
            .concat(
                this.tab.nextWord(state.substring(state.lastIndexOf(" ") + 1))
            );
    }
}

// A generic match function.
interface GenMatch<A> {
    Boolean match(String s, A a);
}

class MatchWord implements GenMatch<WordProb> {

    public Boolean match(String word, WordProb prob) {
        return prob.word.equals(word);
    }
}

// Get an integer value for a type.
interface IntVal<A> {
    int apply(A a);
}

class ProbPairIntVal implements IntVal<ProbPair> {

    public int apply(ProbPair pair) {
        return pair.count;
    }
}

// Probability table.
class ProbTab {

    ILo<WordProb> wordProbs;

    ProbTab(ILo<WordProb> wordProbs) {
        this.wordProbs = wordProbs;
    }

    String nextWord(String state) {
        // The completion probabilities for the current state.
        WordProb stateCompletions = wordProbs.findMatch(new MatchWord(), state);
        String bestWordCompletion = stateCompletions.select();
        return bestWordCompletion;
    }
}

// Word completion probability.
class WordProb {

    String word;
    ILo<ProbPair> probs;

    WordProb(String word, ILo<ProbPair> probs) {
        this.word = word;
        this.probs = probs;
    }

    String select() {
        // Ran out of time, but this should really use a weighted random selection.
        ProbPair bestFit = this.probs.findMax(new ProbPairIntVal());
        return bestFit.word;
    }
}

// A word and its probability (count).
class ProbPair {

    String word;
    int count;

    ProbPair(String word, int count) {
        this.word = word;
        this.count = count;
    }
}
