import Token from '../Token'
import Reader from '../reader'
import * as rules from '../rules'
import { AnnotationInterface } from '../rules/annotation'
import { text, HandlerOptionInterface } from '../handlers'

const SERIES_MARKER = 0x3A /* : */

export function series(reader: Reader, tokens: Token[], silent: boolean = false, options: HandlerOptionInterface): boolean {
    let pos = reader.getCurrentLineStartPos()

    if ( reader.getCharCode(pos++) !== SERIES_MARKER
        || reader.getCharCode(pos++) !== SERIES_MARKER 
        || reader.getCharCode(pos++) !== SERIES_MARKER ) {
            return false
        }

    if (silent) return true
    
    if (options.replMode) {
        options.replMode = false
        reader.nextNonEmptyLines();
    } else {
        reader.nextNonEmptyLines()

        let token: Token

        if (!rules.annotation(reader, true)) {
            text(reader, tokens, false, options)
            
            // We will then need to override the type of token that the text handler created since it should be an REPL token
            // check if we're inside a section token

            if (options.sectionMode) {
                const sectionTokChildren = tokens[tokens.length - 1].children
                token = sectionTokChildren[sectionTokChildren.length - 1]

                token.setType('REPL')
            } else {
                token = tokens[tokens.length - 1]
                token.setType('REPL')
                token.children = []
            }
        }
        else {
            token = new Token('REPL')
            token.setChapter(options.chapter)

            if (options.sectionMode) {
                const sectionTok = tokens[tokens.length - 1]
                token.children = []
                sectionTok.addChild(token)
            } else {
                tokens.push(token)
            }
        }

        if (!rules.annotation(reader, true)) throw new Error(`Expecting an annotation at line ${reader.currentLine}`)

        const annotation = rules.annotation(reader, false)

        token.setReadonly(annotation.readonly === 'true' ? true : false)
        
        if (annotation.caption && annotation.caption.length > 0) {
            token.setCaption(annotation.caption)
        }

        if (annotation.init != null) {
            token.setInitialization(annotation.init)
        }

        if (annotation.type !== 'REPL') throw new Error(`Expecting an REPL annotation at line ${reader.currentLine}`)

        if (annotation.files != null) {
            const files = annotation.files.split(/,\s*/);

            files.forEach((file: string) => {
                token.addFile(file)
            });
        }

        reader.skipEmptyLines()
     
        if (!rules.fence(reader, tokens, true, options)) throw new Error(`REPL type expects a fence markup at line ${reader.currentLine}`)

        rules.fence(reader, tokens, false, options)

        // Let us check if there is another fence block after this.
        while(true) {
            reader.nextNonEmptyLines();

            if (rules.fence(reader, tokens, true, options)) {
                rules.fence(reader, tokens, false, options);
                continue;
            }

            break;
        }

        options.replMode = true                        
    }

    return true
}
