import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * SEO CHECKER DRIVER.
 * This will read in lines from an html file, then calculate
 * a score based on Title tags, meta tags, heading tags, and
 * image tags.
 * @author Alvin Chao
 * @version 3-23-17
 */

public class SEOCheckerDriver {
 
    /**
     * Load the HTML file.
     * 
     * The data file should contain well formed html values.
     * Begin and end tags on one line for counted tags:
     * title, h1, h2, h3.
     * self-closing tags meta and img will also be read in.
     * @param fileName = name of file for input.
     * @return Patron[] array of patrons.
     */
    public static String[] loadFile(String fileName) {
        String[] lines = null;        
        Scanner inFile = null;
        try {
            inFile = new Scanner(new File(fileName));
            int lineCount = countLines(fileName);
            lines = new String[lineCount];
            int i = 0;
            while (inFile.hasNextLine()) {
                lines[i] = inFile.nextLine();
                i++;
            }            
        } catch (IOException e) {
            System.out.println("Error reading html file: " + fileName);
            System.exit(0);
        } finally {
            if (inFile != null) {
                inFile.close();
            }
        }
        return lines;
    }
    
    /**
     * Helper method for determining the number of lines in the file.
     * 
     * @param fileName - Name of the file
     * @return The number of lines in the file
     * @throws FileNotFoundException If the file cannot be opened for reading
     */
    private static int countLines(String fileName)
        throws FileNotFoundException {
        
        int numLines = 0;
        
        Scanner scanner = new Scanner(new File(fileName));
        
        while (scanner.hasNextLine()) {
            scanner.nextLine();
            numLines++;
        }
        return numLines;
    }
    
    /**
     * Main Method.
     * Run the SEO Checker methods and give an SEO Score.
     * @param args = command line arguments.
     */
     
    public static void main(String[] args) {
        String fileName = "testfile.html";
        String[] lines;
        lines = loadFile(fileName);
        //Print file test
        /*
        System.out.println("File: " + fileName);
        for( String line: lines) {
            System.out.println(line);
        }
        */
        double totalScore = 0.0;
        double docTypeScore = 0.0;
        double titleTagScore = 0.0;
        double headingTagScore = 0.0;
        double metaTagScore = 0.0;
        double imageScore = 0.0;
        //Print header
        System.out.println("Page: " + fileName + ".");
        System.out.println("-------------------------");
        //scoreDocType
        docTypeScore = SEOChecker.scoreDocType(lines);
        totalScore += docTypeScore;
        System.out.println("DocType score(10):\t\t\t" + docTypeScore);
        //scoreHeaderTags
        headingTagScore = SEOChecker.scoreHeadingTags(lines);
        totalScore += headingTagScore;
        System.out.println("Heading Tag score(10):\t\t" 
            + headingTagScore);        
        //scoreMeta
        metaTagScore = SEOChecker.scoreMetaTags(lines);
        totalScore += metaTagScore;
        System.out.println("Meta Tag score(10):\t\t\t" + metaTagScore);
        //scoreImages
        imageScore = SEOChecker.scoreImageTags(lines);
        totalScore += imageScore;
        System.out.println("Image score(10):\t\t\t" + imageScore);
        System.out.println("-------------------------");
        //output final score to user.
        System.out.printf("Website total score(40):\t%.2f  = %.2f%%",
            totalScore, totalScore / 40 * 100);
    }
}
