# -*- coding: utf-8 -*-
# Copyright (c) 2012 The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details.
"""
pycroft.lib.user
~~~~~~~~~~~~~~

This module contains.

:copyright: (c) 2012 by AG DSN.
"""

from datetime import datetime
from flask.ext.login import current_user
from pycroft.helpers import user_helper, host_helper
from pycroft.model.dormitory import Dormitory, Room, Subnet, VLan
from pycroft.model.hosts import Host, NetDevice
from pycroft.model.logging import UserLogEntry
from pycroft.model.session import session
from pycroft.model.user import User


def moves_in(name, login, dormitory, level, room_number, host_name, mac):
    ip_address = host_helper.get_free_ip(dormitory.get_subnets())

    if not host_name:
        host_name = host_helper.generate_hostname(ip_address)

    room = Room.q.filter_by(number=room_number,
        level=level, dormitory_id=dormitory.id).one()

    #ToDo: Which port to choose if room has more than one?
    patch_port = room.patch_ports[0]

    new_user = User(login=login,
        name=name,
        room=room,
        registration_date=datetime.now())
    plain_password = user_helper.generatePassword(12)

    #TODO: DEBUG remove in productive!!!
    print u"new password: " + plain_password
    new_user.set_password(plain_password)
    session.add(new_user)

    new_host = Host(hostname=host_name,
        user=new_user,
        room=room)
    session.add(new_host)

    new_net_device = NetDevice(ipv4=ip_address,
        mac=mac,
        host=new_host,
        patch_port=patch_port)
    session.add(new_net_device)
    session.commit()

    return new_user


def move(user, dormitory, level, room_number):
    oldRoom = user.room
    newRoom = Room.q.filter_by(number=room_number,
        level=level,
        dormitory_id=dormitory.id).one()
    user.room = newRoom
    session.add(user)

    newUserLogEntry = UserLogEntry(author_id=current_user.id,
        message=u"umgezogen von %s nach %s" % (oldRoom, newRoom),
        timestamp=datetime.now(), user_id=user.id)
    session.add(newUserLogEntry)

    if oldRoom.dormitory_id != newRoom.dormitory_id:
        #TODO let choose which hosts should move in the same room
        netdevices = session.query(
            NetDevice
        ).join(
            NetDevice.host
        ).filter(
            Host.user_id == user.id
        ).all()

        for netdevice in netdevices:
            #TODO set new patchport
            oldIPv4 = netdevice.ipv4
            netdevice.ipv4 = host_helper.get_free_ip(dormitory.get_subnets())
            session.add(netdevice)
            newUserLogEntry = UserLogEntry(author_id=current_user.id,
                message=u"IPv4 von %s auf %s geändert" % (
                oldIPv4, netdevice.ipv4),
                timestamp=datetime.now(), user_id=user.id)
            session.add(newUserLogEntry)

    session.commit()
    return user


def edit_name(user, name):
    oldName = user.name
    if len(name):
        user.name = name
    session.add(user)

    newUserLogEntry = UserLogEntry(author_id=current_user.id,
        message=u"Nutzer %s umbenannt in %s" % (oldName, name),
        timestamp=datetime.now(), user_id=user.id)
    session.add(newUserLogEntry)

    session.commit()

    return user

@@ -559,17 +559,21 @@
 etDevice
+, Ip
 %0A
-
 from pyc
@@ -765,16 +765,54 @@
 , mac):%0A
+    subnets = dormitory.get_subnets()%0A
     ip_a
@@ -844,39 +844,90 @@
 free_ip(
-dormitory.get_
+subnets)%0A    subnet = host_helper.select_subnet_for_ip(ip_address, 
 subnets
-()
 )%0A%0A    i
@@ -1168,16 +1168,65 @@
 an one?%0A
+    # --%3E The one that is connected to a switch!%0A
     patc
@@ -1722,49 +1722,16 @@
 ice(
-ipv4=ip_address,%0A        mac=mac,%0A       
+mac=mac,
  hos
@@ -1741,24 +1741,16 @@
 ew_host,
-%0A       
  patch_p
@@ -1765,16 +1765,95 @@
 h_port)%0A
+    new_ip = Ip(net_device=new_net_device, address=ip_address, subnet=subnet)%0A%0A
     sess
@@ -1872,24 +1872,49 @@
 net_device)%0A
+    session.add(new_ip)%0A%0A
     session.
@@ -2769,59 +2769,106 @@
     
-oldIPv4 = netdevice.ipv4%0A            netdevice.ipv4
+for ip_addr in netdevice.ips:%0A                old_ip = ip_addr.address%0A                new_address
  = h
@@ -2931,31 +2931,237 @@
     
+  
-session.add(netdevice)%0A
+  new_subnet = host_helper.select_subnet_for_ip(new_address,%0A                                                    dormitory.get_subnets())%0A            %0A                ip_addr.change_ip(new_address, new_subnet)%0A            %0A    
     
@@ -3218,32 +3218,36 @@
 urrent_user.id,%0A
+    
                 
@@ -3309,34 +3309,38 @@
     
-oldIPv4, netdevice.ipv4),%0A
+    old_ip, new_address),%0A    
     
@@ -3390,24 +3390,28 @@
 id=user.id)%0A
+    
             

Modify the user lib funcs for the new Ip model