import os

from aiohttp import web

from cdtz import config

LIST_TZ = []


def create_list_tz():
    """Create list timezones. """

    dir_zoneinfo = '/usr/share/zoneinfo'
    for dirpath, dirnames, filenames in os.walk(dir_zoneinfo):
        for file in filenames:
            filepath = os.path.join(dirpath, file)
            if open(filepath, 'rb').read(4) == b'TZif':
                filename = filepath.replace(f'{dir_zoneinfo}/', '')
                if filename not in ['Factory', 'localtime', 'posixrules', 'Riyadh8']:
                    LIST_TZ.append(filename)

    LIST_TZ.sort()


async def get_list_tz(request):
    """Returns list timezone. """

    return web.json_response(LIST_TZ)


async def main():
    """The main entry point. """

    app = web.Application()
    app.router.add_get('/tz/list_time_zones', get_list_tz)
    return app


if __name__ == '__main__':
    create_list_tz()
    web.run_app(main(), host=config.HOST, port=config.PORT)
