# -*- coding: utf-8 -*-
#
# Author: Den Schigrov <dennytwix@gmail.com>
# Created: 2/21/15
#
# Id: $
import math
from settings import SCENE_SIZE, MIN_ACCEL, MAX_ACCEL

__author__ = 'den'
from position import Position


class Shoot(object):
    user = None
    target = None
    position = None
    speed = None
    strength = None
    scene = None

    def __init__(self, user, target, x=None, y=None, strength=1, speed=None):
        self.speed = speed
        self.user = user
        self.target = target
        self.strength = strength

        if x and y:
            position = Position(x, y)
            self.position = position
        else:
            self.position = self.target.position

        if self.speed:
            self.proceed_target()

    def proceed_target(self):
        if self.speed > MIN_ACCEL:
            self.speed = min(self.speed, MAX_ACCEL)
            max_accel_distance = round(math.sqrt(2 * SCENE_SIZE * SCENE_SIZE))
            distance = (float(self.speed) - MIN_ACCEL) / (MAX_ACCEL - MIN_ACCEL) * max_accel_distance

            delta_x = self.target.position.x - self.user.position.x
            delta_y = self.target.position.y - self.user.position.y

            target_distance = math.sqrt(delta_x * delta_x + delta_y * delta_y)
            k = float(distance) / target_distance

            relative_delta_x = delta_x * k
            relative_delta_y = delta_y * k

            abs_relative_x = abs(relative_delta_x)
            abs_relative_y = abs(relative_delta_y)

            if abs_relative_x < 0.5 and abs_relative_y < 0.5:
                if abs_relative_x > abs_relative_y:
                    if relative_delta_x > 0:
                        relative_delta_x = math.ceil(relative_delta_x)
                    else:
                        relative_delta_x = math.floor(relative_delta_x)
                else:
                    if relative_delta_y > 0:
                        relative_delta_y = math.ceil(relative_delta_y)
                    else:
                        relative_delta_y = math.floor(relative_delta_y)
            else:
                relative_delta_x = round(relative_delta_x)
                relative_delta_y = round(relative_delta_y)

            new_x = self.user.position.x + relative_delta_x
            new_y = self.user.position.y + relative_delta_y

            # new_x = min(new_x, SCENE_SIZE - 1)
            # new_x = max(0, new_x)

            # new_y = min(new_y, SCENE_SIZE - 1)
            # new_y = max(0, new_y)

            new_x, new_y = int(new_x), int(new_y)

            self.target = self.scene.cells[new_x][new_y]
            self.position = Position(new_x, new_y)

    def json(self):
        return {
            'start_position': self.user.position.json() if self.user.position else None,
            'position': self.position.json() if self.position else None,
            'start_user_id': self.user.id,
            'target_user_id': self.target.id if self.target else None,
            'target_user_health': self.target.health if self.target else None
        }