#!/usr/bin/python3
import argparse
import http.server
import json
import os
import re
import shutil
import socketserver


class Builder:
    def __init__(self):
        self.config_file_path = 'homemaker.conf'
        self.template_path = 'templates/template.html'
        self.build_directory = 'build'

        self.key_map = {}
        self.bg_map = {}
        self.bg_options = ''
        self.links = ''

        self.style_map = {
            'title': 'Homepage',
            'bg_img': 'null',
            'font': 'monospace',
            'font_color': '#e6e6fa',
            'font_size': '25',
            'link_color': '#e6e6fa',
            'link_hover_color': '#ff3f33',
        }

        self.section_count = 0
        self.section_links = ''
        self.section_array = []
        self.section_map = {}

    def main(self):
        self.handle_backgrounds()
        self.set_up_build_dir()
        self.parse_config_file()
        self.cleanup()
        self.generate_files()
        self.copy_bgs()

    def set_up_build_dir(self):
        if os.path.exists(self.build_directory):
            print('Deleting build directory...')
            shutil.rmtree(self.build_directory)

        print('Creating build directory...')
        os.makedirs(self.build_directory)

    def handle_backgrounds(self):
        print('Parsing background images...')
        for root, dirs, files in os.walk("bg/", topdown=False):
            for idx, file_path in enumerate(files):

                file_name = file_path.split('/')[-1].split('.')[0]
                if idx == 0:
                    self.style_map['bg_img'] = file_path
                self.bg_map[file_name] = file_path
                option = f'\t<option value="{file_name}">{file_name}</option>\n'
                self.bg_options += option

    def parse_config_file(self):
        print('Parsing config file...')
        with open(self.config_file_path) as f:
            mode = None
            for line in f:
                line = line.strip()

                if line == "[style]":
                    print('Handling styles...')
                    mode = 'style'
                    continue

                if line == "[links]":
                    print('Handling links...')
                    mode = 'links'
                    continue

                if mode == 'links':
                    self.handle_links(line)
                elif mode == 'style':
                    self.handle_styles(line)

    def handle_styles(self, line):
        style = re.split(r'[ \t]+', line)
        if style[0] in self.style_map:
            self.style_map[style[0]] = style[1]

    def handle_links(self, line):
        links = re.split(r'[ \t]+', line)
        if links[0] == 'section':
            self.section_count += 1

            self.section_links += f'<a href="#" class="link" onclick="toggleSection("{links[1]}")">[{self.section_count}]&nbsp;{links[1]}</a> '
            self.section_array.append(links[1])

            if self.links == '':
                self.links += f'<div id="{links[1]}" style="display:block">\n'
            else:
                self.links += f'<div id="{links[1]}" style="display:none">\n'

            self.section_map[str(self.section_count)] = links[1]

        elif links[0] == '':
            self.links += '</div>\n'

        else:
            self.links += f'\t<a href="{links[2]}" id="{links[1]}" class="link">[{links[0]}] {links[1]} </a><br>\n'

            self.key_map[links[0]] = links[2]

    def cleanup(self):
        self.links += '</div>\n\n'

    def generate_files(self):
        print('Generating Files...')
        with open(self.template_path) as f:
            compiled_file = f.read() \
                .replace('{{bg_options}}', self.bg_options) \
                .replace('{{links}}', self.links) \
                .replace('{{section_links}}', self.section_links) \
                .replace('{{bg_map}}', json.dumps(self.bg_map)) \
                .replace('{{key_map}}', json.dumps(self.key_map)) \
                .replace('{{section_array}}', json.dumps(self.section_array)) \
                .replace('{{section_map}}', json.dumps(self.section_map))

            for style, value in self.style_map.items():
                compiled_file = compiled_file.replace('{{' + style + '}}', value)

        with open(self.build_directory + '/index.html', 'w+') as f:
            f.write(compiled_file)

    def copy_bgs(self):
        print('Copying backgrounds..')
        shutil.copytree('bg/', self.build_directory + '/bg/')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('action', type=str, help='OPTIONS:\n build - delete and recompile your build folder. \n serve - run webserver')

    args = parser.parse_args()

    if args.action == 'build':
        b = Builder()
        b.main()
    elif args.action == 'serve':
        os.chdir('build/')
        PORT = 8000
        Handler = http.server.SimpleHTTPRequestHandler

        with socketserver.TCPServer(("", PORT), Handler) as httpd:
            print(f"Check out your homepage here: http://localhost:{PORT}")
            httpd.serve_forever()
