<?php

namespace CheckPointTypes;

class CheckPointType
{
    public $id;
    public $priority;
    public $name;
    public $parentTaskTypeId;
    public $parentCheckpointTypeId;
    public $childrenCheckpointTypes;
    public $class;
    private $write_to_twx;

    function __construct(){}

    public function setWriteToTWX(int $val)
    {
        global $link;
        $this->write_to_twx = $val;
        mysqli_query($link, "update checkpoint_types set write_to_twx=$val where id=$this->id");
    }

    public function getWriteToTWX()
    {
        return $this->write_to_twx;
    }

    public static function CreateFromArray($checkpointArr)
    {
        $checkpointType = new CheckPointType();
        $checkpointType->id = $checkpointArr['id'];
        $checkpointType->name = $checkpointArr['name'];
        $checkpointType->class = $checkpointArr['class'];
        $checkpointType->write_to_twx = $checkpointArr['write_to_twx'];
        $checkpointType->childrenCheckpointTypes = array();
        $checkpointType->FillChildren();
        return $checkpointType;
    }

    public static function CreateFromScratch($class, $name, $parentTaskTypeId = null, $write_to_twx = 0)
    {
        global $link;
        $checkpointType = new CheckPointType();

        $checkpointType->name = $name;
        $checkpointType->class = $class;
        $checkpointType->childrenCheckpointTypes = array();
        mysqli_query($link, "insert into checkpoint_types (class, name, write_to_twx) values ('$class', '$name', $write_to_twx)");
        $checkpointType->id = mysqli_insert_id($link);
        if ($parentTaskTypeId != null) {
            mysqli_query($link, "insert into checkpoint_type_hierarchy (parent_cp_type, cp_type) values ($parentTaskTypeId, $checkpointType->id)");
            //mysqli_query($link, "update checkpoint_type_hierarchy set parent_cp_type=$parentTaskTypeId, cp_type=$checkpointType->id");
        }
        return $checkpointType;
    }

    static function AddChildToDB($id, $parentTaskTypeId)
    {
        global $link;
        mysqli_query($link, "insert into checkpoint_type_hierarchy (parent_cp_type, cp_type) values ($parentTaskTypeId, $id)");
    }

    function RemoveChildFromDB($id)
    {
        global $link;
        mysqli_query($link, "delete from checkpoint_type_hierarchy where parent_cp_type=$this->id and cp_type=$id");
    }

    public static function CreateFromID($id)
    {
        global $link;
        $query = mysqli_query($link, "select * from checkpoint_types where id=$id");
        if ($checkpoint_arr = mysqli_fetch_array($query))
        {
            $cpType = CheckPointType::CreateFromArray($checkpoint_arr);
            return $cpType;
        }
        return null;
    }

    public static function GetCheckPointTypes($class = null)
    {
        global $link;
        $checkpointTypes = array();  //массив значений чекпойнтов
        $add = '';
        if ($class != null)
            $add =  "where class='$class'";
        $query = mysqli_query($link, "select * from checkpoint_types $add ORDER BY name");
        while ($checkpoint_arr = mysqli_fetch_array($query))
        {
            $cpType = CheckPointType::CreateFromArray($checkpoint_arr);
            array_push($checkpointTypes, $cpType);
        }
        return $checkpointTypes;
    }

    public static function GetCheckPointTypesByTask($task_type = null)
    {
        global $link;
        $checkpointTypes = array();  //массив значений чекпойнтов
        $add = '';
        if ($task_type != null)
            $add =  "where tasktype_id=$task_type";
        $query = mysqli_query($link, "select * from checkpoint_types_for_tasks $add");
        while ($checkpoint_arr = mysqli_fetch_array($query))
        {
            array_push($checkpointTypes, $checkpoint_arr);
        }
        return $checkpointTypes;
    }

    public static function CheckPointTemplate($parent_checkpoint_type)
    {
        global $link;
        $checkpointTypes = array();  //массив значений чекпойнтов
        $query = mysqli_query($link, "select * from checkpoint_hierarchy where parent_cp_type=$parent_checkpoint_type");
        while ($checkpoint_arr = mysqli_fetch_array($query))
        {
            array_push($checkpointTypes, $checkpoint_arr);
        }
        return $checkpointTypes;
    }

    public static function Delete($id)
    {
        $cpt = self::CreateFromID($id);
        foreach ($cpt->childrenCheckpointTypes as $child)
        {
            $cpt->RemoveChildFromDB($child->id);
        }
        $cpt->DeleteFromDB();
    }

    function DeleteFromDB()
    {
        global $link;
        mysqli_query($link, "delete from checkpoint_types where id=$this->id");
        mysqli_query($link, "delete from checkpoint_types_for_tasks where cp_type_id=$this->id");
    }

    function GetChildrenIDs()
    {
        global $link;
        $children_ids = array();
        $query = mysqli_query($link, "select * from checkpoint_type_hierarchy where parent_cp_type=".$this->id);
        while ($checkarr = mysqli_fetch_array($query))
        {
            array_push($children_ids, $checkarr);
        }
        return $children_ids;
    }

    function FillChildren()
    {
        global $link;
        $children_ids = array();
        $query = mysqli_query($link, "select * from checkpoint_type_hierarchy where parent_cp_type=".$this->id);
        while ($checkarr = mysqli_fetch_array($query))
        {
            array_push($children_ids, $checkarr);
        }
        foreach ($children_ids as $child)
        {
            $query = mysqli_query($link, "select * from checkpoint_types where id=".$child['cp_type']);
            while ($checkarr = mysqli_fetch_array($query))
            {
                $child = CheckPointType::CreateFromArray($checkarr);
                array_push($this->childrenCheckpointTypes, $child);
            }
        }
    }
}