<?php defined('SYSPATH') || die('No direct script access.');

class Controller_Handler_Discipline extends Controller_Handler_Api
{
    /** @var  Model_User_Teacher */
    protected $user;

    # /handler/discipline/
    public function action_index() {
        $id = (int) $this->request->post('id');
        return Model_Discipline::load($id);
    }

    # /handler/discipline/create
    public function action_create() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $discipline = Model_Discipline::make()
            ->author  ($this->user->TeacherID)
            ->semester($this->post['semesterID'])
            ->faculty ($this->post['facultyID'])
            ->subject ($this->post['subjectID'])
            ->grade   ($this->post['gradeID'])
            ->lectures($this->post['lectures'])
            ->practice($this->post['practice'])
            ->labs    ($this->post['labs'])
            ->type    ($this->post['type'])
            ->create();

        if ($this->post['bonus'] == "true")
            Model_Map::of($discipline)->addModuleBonus($this->user->TeacherID);

        return ['ID' => $discipline->ID];
    }

    public function action_getCompounds() {
        $ret = '<option value="0"> -- Нет -- </option>';
        $compounds = Model_Grades::getCompoundDisciplinesForGrade($this->post['gradeID']);
        foreach($compounds as $cmp){
            $cmpID = $cmp['ID'];
            $cmpName = $cmp['Name'];
            $ret .= "<option value = $cmpID> $cmpName </option>";
        }
        return $ret;
    }

    # /handler/discipline/update
    public function action_update() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int) $this->request->post('id');

        if ($id <= 0)
            throw new InvalidArgumentException(Error::ID_IS_INCORRECT);

        throw new LogicException('Not implemented yet');
    }

    # /handler/discipline/clear
    public function action_clear() {
        # todo: implement me
        $this->user->checkAccess(User::RIGHTS_ADMIN);

        $id = (int) $this->request->post('id');
//        Model_Discipline::load($id)->clear()->delete();
    }

    # /handler/discipline/delete
    public function action_delete() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $discipline = $this->loadDisciplineAndAuthorCheck();
        if ($discipline->IsLocked)
            throw new LogicException(Error::DISCIPLINE_IS_LOCKED);
        if (Model_Rating::count($discipline) > 0)
            throw new LogicException(Error::DISCIPLINE_IS_LOCKED);

        $discipline->delete();
    }

    public function action_delegate() {
//        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
//            throw HTTP_Exception::factory(404);

        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->delegateTo($teacher);
    }

    public function action_bind() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->bind($teacher);
    }

    public function action_unbind() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->unbind($teacher);
    }

    public function action_copyStructure() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->request->post('teacherID');
        $disciplineFromID = (int) $this->request->post('disciplineFromID');
        $discipline->copyDisciplineStructureFrom($teacherID, $disciplineFromID);
    }

    public function action_getSimilarDisciplines() {
        $this->loadDisciplineAndAuthorCheck();
        $subjectID = (int) $this->request->post('subjectID');
        $teacherID = (int) $this->request->post('teacherID');
        return json_encode(Model_Teacher::with($teacherID)->getSimilarDisciplines($subjectID));
    }

    public function action_getDisciplinesForGroup() {
        $groupID = $this->request->post('groupID');
        $semesterID = User::instance()->SemesterID;
        //$students = Model_Students::ofGroup($groupID, $semesterID);

        // TODO: refactor after new groups implemented
        $disciplines = Model_Group::with($groupID)->getDisciplinesForExport(false, $semesterID);
        $disciplines = Arr::groupByUniqueKey('ID', $disciplines);

        return json_encode($disciplines);
    }

    /**
     * @return Model_Discipline
     */
    private function loadDisciplineAndAuthorCheck() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int)$this->request->post('id');
        $discipline = Model_Discipline::load($id);
        if ($discipline->AuthorID != $this->user->TeacherID)
            throw new LogicException(Error::ACCESS_DENIED);
        return $discipline;
    }
}