#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
import math

def translation(i, j, x, y):
    print("Translation along vector ({}, {})" .format(i, j))

def scaling(m, n, x, y):
    print("Scaling by factors {} and {}" .format(m, n))

def rotation(gamma, x, y):
    print("Rotation by a {} degree angle" .format(gamma))

def reflection(gamma, x, y):
    print("Reflection over an axis with an inclination angle of {} degrees" .format(gamma))

def print_help():
    print("USAGE")
    print("\t./102architect x y transfo1 arg11 [arg12] [transfo2 arg12 [arg22]] ...")
    print("\n\nDESCRIPTION")
    print("\tx \tabscissa of the original point")
    print("\ty \tordinate of the original point\n")
    print("\ttransfo arg1 [arg2]")
    print("\t-t i j\t\ttranslation along vector (i, j)")
    print("\t-z m n\t\tscaling by factors m (x-axis) and n (y-axis)")
    print("\t-r d\t\trotation centered in O by a d degree angle")
    print("\t-s d\t\treflection over the axis passing through O with an inclination angle of d degrees")

def product_matrix(matrice, matrice_bis):
    res = 0
    count = 0
    line = 0
    new_vect = [0.00, 0.00, 1.00]

    for i in matrice:
        for j in i:
            res += float(j) * float(matrice_bis[count])
            count+= 1
        new_vect[line] = res
        res = 0
        count = 0
        line+=1
    return new_vect

def main():
    if len(sys.argv) < 4 :
        print("not enough arguments")
        sys.exit(84)
    if sys.argv.__contains__("-h"):
        print_help()
        sys.exit(0)

    try :
        vect =  {0: int(sys.argv[1]),
                    1 : int(sys.argv[2]),
                    2 : 1 }

    except ValueError:
        sys.exit(84)

    matrice = [[1.00, 0.00, 0.00],
                [0.00, 1.00, 0.00],
                [0.00, 0.00, 1.00]]

    for i in range(len(sys.argv)):
        if sys.argv[i] == "-t":
            try:
                translation(int(sys.argv[i + 1]), int(sys.argv[i + 2]), vect[0], vect[1])
            except:
                print("error with translation arguments")
                sys.exit(84)
            matrice[0][2] = float(sys.argv[i + 1])
            matrice[1][2] = float(sys.argv[i + 2])
            vect = product_matrix(matrice, vect)

        if sys.argv[i] == "-z":
             scaling(int(sys.argv[i + 1]), int(sys.argv[i + 2]), vect[0], vect[1])
             matrice[0][2] = 0
             matrice[1][2] = 0
             matrice[0][0] = float(sys.argv[i + 1])
             matrice[1][1] = float(sys.argv[i + 2])
             vect = product_matrix(matrice, vect)

        if sys.argv[i] == "-r":
            rotation(int(sys.argv[i + 1]), vect[0], vect[1])
            matrice[0][0] = math.cos(float(sys.argv[i + 1]) / 180 * math.pi)
            matrice[0][1] = -math.sin(float(sys.argv[i + 1]) / 180 * math.pi)
            matrice[1][0] = math.sin(float(sys.argv[i + 1]) / 180 * math.pi)
            matrice[1][1] = math.cos(float(sys.argv[i + 1]) / 180 * math.pi)
            vect = product_matrix(matrice, vect)

        if sys.argv[i] == "-s" :
            reflection(int(sys.argv[i + 1]), vect[0], vect[1])
            matrice[0][0] = math.cos(2 * float(sys.argv[i + 1]) / 180 * math.pi)
            matrice[0][1] = math.sin(2 * float(sys.argv[i + 1]) / 180 * math.pi)
            matrice[1][0] = math.sin(2 * float(sys.argv[i + 1]) / 180 * math.pi)
            matrice[1][1]= -math.cos(2 * float(sys.argv[i + 1]) / 180 * math.pi)
            vect = product_matrix(matrice,vect)

    print("{:0.2f}\t{:0.2f}\t{:0.2f}" .format(matrice[0][0], matrice[0][1], matrice[0][2]))
    print("{:0.2f}\t{:0.2f}\t{:0.2f}" .format(matrice[1][0], matrice[1][1], matrice[1][2]))
    print("{:0.2f}\t{:0.2f}\t{:0.2f}" .format(matrice[2][0], matrice[2][1], matrice[2][2]))

    print("({}, {}) => ({:0.2f}, {:0.2f})" .format(sys.argv[1], sys.argv[2], vect[0], vect[1]))

main()
