# -*- coding: utf-8 -*-
#
# New BSD license
#
# Copyright (c) DR0ID
# This file '__main__.py' is part of pw-40
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL DR0ID BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import print_function, division

import faulthandler
import logging

import pygame
from pygame import Surface

from game.game import Game

SCREENSIZE = 1024, 768


def main():
    faulthandler.enable()
    # args, parser = initialize_argument_parser()
    initialize_logging(None)
    logger = logging.getLogger(__file__)
    try:
        logger.info(">>>>> starting >>>>>")

        # force to stereo to avoid crashes, see:
        # https://pyga.me/docs/ref/mixer.html#pygame.mixer.Channel.set_source_location
        pygame.mixer.pre_init(channels=2, allowedchanges=pygame.AUDIO_ALLOW_FREQUENCY_CHANGE)

        pygame.init()
        # screen = pygame.display.set_mode(SCREENSIZE, pygame.RESIZABLE | pygame.SRCALPHA, 32)
        pygame.display.set_icon(pygame.image.load("game/data/icon.png"))
        pygame.display.set_caption("Skyscraper City Uptown", "SkyCity")
        screen: Surface = pygame.display.set_mode(SCREENSIZE)

        game_main = Game()
        game_main.run(screen)

        pygame.quit()

    except Exception as ex:
        logger.error("error: %s", ex)
        raise


if __name__ == '__main__':
    main()

_log_levels = {
    'critical': logging.CRITICAL,
    'error': logging.ERROR,
    'warn': logging.WARNING,
    'warning': logging.WARNING,
    'info': logging.INFO,
    'debug': logging.DEBUG
}

log_file_name = "run_game.py.log"


def initialize_logging(args):
    log_level = logging.INFO if __debug__ else logging.WARNING  # _log_levels[args.log_level]
    formatter = logging.Formatter(
        fmt="%(asctime)s %(levelname)s: %(message)s   [%(funcName)s in "
            "%(filename)s(%(lineno)d)] (pid:%(process)d tid:%(thread)d %(threadName)s)")
    handlers = []
    # if not args.log_no_file:
    #     rotating_handler = RotatingFileHandler(log_file_name, backupCount=10, maxBytes=1024 * 1024 * 10)
    #     rotating_handler.setFormatter(formatter)
    #     rotating_handler.setLevel(log_level)
    #     handlers.append(rotating_handler)
    if True:  # args.log_console:
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)
        stream_handler.setLevel(log_level)
        handlers.append(stream_handler)
    logging.basicConfig(handlers=handlers, level=logging.DEBUG)
    logging.setLogRecordFactory(logging.LogRecord)
