<?php

class WizardStepAdminUser extends WizardStep
{

    public function run()
    {   
        app()->user->logout();
        
        $adminUserForm = new AdminUserForm();
        
        $admin = $this->wizardController->getAdminUser();
        
        $this->setRelevantAttributes($adminUserForm, $admin);
        $adminUserForm->unsetAttributes(array("password", "confirmPassword", "pin"));
        $this->performAjaxValidation($admin, 'admin-user-form');
        
        
        if (isset($_POST["AdminUserForm"])) {
            
            
            $sipAccount = SipAccounts::model()->findByAttributes(array(
                "authname" => $admin->extension,
                "user_id" => $admin->id
            ));
            
            $adminUserForm->attributes = $_POST["AdminUserForm"];
            
            if ($adminUserForm->validate()) {
                $this->updateAdminUser($admin, $adminUserForm);
                $this->updateDropTarget($adminUserForm);
                $this->updateReferenceAddress($admin, $adminUserForm);
                $this->updateAdminSipAccount($admin, $sipAccount);
                $this->loginUser($adminUserForm);
                $this->setStepIsDone(true);
                return;
            }
        }
        
        $this->wizardController->render("adminUser", array("admin" => $adminUserForm));
    }
    
    private function updateAdminUser(&$admin, $form)
    {
        
        unset(Yii::app()->request->cookies['language']);
        
        Yii::app()->user->id = $admin->id;
        
        $this->setRelevantAttributes($admin, $form);
        
        if (!$admin->save()) {    
            throw new CHttpException(500, "Internel server Error: unable to update admin user!");
        }
    }
    
    private function updateDropTarget($formModel) {
        setCfg('DP_DROPTARGET', $formModel->extension);
    }


    private function setRelevantAttributes(&$admin, $form)
    {
        $admin->name = $form->name;
        $admin->password = $form->password;
        $admin->firstname= $form->firstname;
        $admin->lastname = $form->lastname;
        $admin->extension = $form->extension;
        $admin->email= $form->email;
        $admin->pin= $form->pin;
    }
    
    private function updateReferenceAddress($admin, $form)
    {
        $address = Addresses::model()->findByPk($admin->addressReferece);
        if ($address) { 
            $address->n_given = $form->firstname != null ? $form->firstname : $form->name;
            $address->n_family = $form->lastname != null ? $form->lastname : "";
            $address->contact_email = $form->email;
            $address->tel_work = $form->extension;
            $address->save();
        }
    }
    
    private function updateAdminSipAccount($admin, $sipAccount = NULL) {
        
        if (!$sipAccount) {
            $sipAccount = SipAccounts::generateSipAccountForUser($admin);

        } else {
            $sipAccount->authname = $admin->extension;
        }
        
        if (!$sipAccount->save()) {
            dump($sipAccount->errors);
            die();
            throw new CHttpException(404, 'Can not save admin sipaccount, please restart initializer.');
        }
    }
    
    private function loginUser($form) {
        $loginForm = new LoginForm();
        $loginForm->username = $form->name;
        $loginForm->password = $form->password;
        if ( $loginForm->validate() && $loginForm->login()) {
            return true;
        }
        return false;
    }
}

