<?php

/**
* MILESTONE
*/
class milestone
{
    private $ms_id;
    private $ms_type_mt_id;
    private $ms_training_mst_id = null;
    private $ms_competition_msc_id = null;
    private $ms_camp_msca_id = null;
    private $ms_ranking_msr_id = null;
    private $ms_user_kid_uk_id;
    private $ms_sums; //text

    private $season;

    public function set_ms_data_by_id($_ms_id) {
        global $sql;
        $cat_assoc_array = $sql->assoc_array("select * from milestone where ms_id = " . $_ms_id);
        $cat_array = $cat_assoc_array[0];
        //alapadatok
        foreach ($cat_array as $field => $value) {
            $function_name = "set_" . $field;
            $this->$function_name($value);

            if ($field == 'ms_type_mt_id' && $value != null) {
                $new_mt = new milestone_type();
                $new_mt->set_mt_data_by_id($value);
                $this->set_ms_type_mt_id($new_mt);
            }

            if ($field == 'ms_training_mst_id' && $value != null) {
                $new_mst = new milestone_training();
                $new_mst->set_mst_data_by_id($value);
                $this->set_ms_training_mst_id($new_mst);
            }

            if ($field == 'ms_competition_msc_id' && $value != null) {
                $new_msc = new milestone_competition();
                $new_msc->set_msc_data_by_id($value);
                $this->set_ms_competition_msc_id($new_msc);
            }

            if ($field == 'ms_camp_msca_id' && $value != null) {
                $new_msca = new milestone_camp();
                $new_msca->set_msca_data_by_id($value);
                $this->set_ms_camp_msca_id($new_msca);
            }

            if ($field == 'ms_ranking_msr_id' && $value != null) {
                $new_msr = new milestone_ranking();
                $new_msr->set_msr_data_by_id($value);
                $this->set_ms_ranking_msr_id($new_msr);
            }

            if ($field == 'ms_season' && $value != null) {
                $new_mss = new milestone_season();
                $new_mss->set_mss_data_by_id($value);
                $this->set_ms_season($new_mss);
            }
        }
    }

    public static function create_ms($_mt, $_season, $_uk, $_mst = null, $_msc = null, $_msca = null, $_msr = null, $_sums = null)
    {
        global $sql;
        return $sql->insert_into('milestone', array(
            'ms_type_mt_id' => $_mt,
            'ms_season' => $_season,
            'ms_user_kid_uk_id' => $_uk,
            'ms_training_mst_id' => $_mst,
            'ms_competition_msc_id' => $_msc,
            'ms_ranking_msr_id' => $_msr,
            'ms_camp_msca_id' => $_msca,
            'ms_sums' => $_sums,
        ), false);
    }

    public function get_ms_month() {
        if (null !== $this->ms_competition_msc_id) {
            return $this->ms_competition_msc_id->get_msc_month();
        }
        elseif (null !== $this->ms_training_mst_id) {
            return $this->ms_training_mst_id->get_mst_month();
        }
        elseif (null !== $this->ms_camp_msca_id) {
            return $this->ms_camp_msca_id->get_msca_month();
        }
    }



    /**
     * @return mixed
     */
    public function get_ms_id()
    {
        return $this->ms_id;
    }

    /**
     * @param mixed $ms_id
     *
     * @return self
     */
    public function set_ms_id($ms_id)
    {
        $this->ms_id = $ms_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_season()
    {
        return $this->ms_season;
    }

    /**
     * @param mixed $ms_season
     *
     * @return self
     */
    public function set_ms_season($ms_season)
    {
        $this->ms_season = $ms_season;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_type_mt_id()
    {
        return $this->ms_type_mt_id;
    }

    /**
     * @param mixed $ms_type_mt_id
     *
     * @return self
     */
    public function set_ms_type_mt_id($ms_type_mt_id)
    {
        $this->ms_type_mt_id = $ms_type_mt_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_training_mst_id()
    {
        return $this->ms_training_mst_id;
    }

    /**
     * @param mixed $ms_training_mst_id
     *
     * @return self
     */
    public function set_ms_training_mst_id($ms_training_mst_id)
    {
        $this->ms_training_mst_id = $ms_training_mst_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_competition_msc_id()
    {
        return $this->ms_competition_msc_id;
    }

    /**
     * @param mixed $ms_competition_msc_id
     *
     * @return self
     */
    public function set_ms_competition_msc_id($ms_competition_msc_id)
    {
        $this->ms_competition_msc_id = $ms_competition_msc_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_camp_msca_id()
    {
        return $this->ms_camp_msca_id;
    }

    /**
     * @param mixed $ms_camp_msca_id
     *
     * @return self
     */
    public function set_ms_camp_msca_id($ms_camp_msca_id)
    {
        $this->ms_camp_msca_id = $ms_camp_msca_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_ranking_msr_id()
    {
        return $this->ms_ranking_msr_id;
    }

    /**
     * @param mixed $ms_ranking_msr_id
     *
     * @return self
     */
    public function set_ms_ranking_msr_id($ms_ranking_msr_id)
    {
        $this->ms_ranking_msr_id = $ms_ranking_msr_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_user_kid_uk_id()
    {
        return $this->ms_user_kid_uk_id;
    }

    /**
     * @param mixed $ms_user_kid_uk_id
     *
     * @return self
     */
    public function set_ms_user_kid_uk_id($ms_user_kid_uk_id)
    {
        $this->ms_user_kid_uk_id = $ms_user_kid_uk_id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function get_ms_sums()
    {
        return $this->ms_sums;
    }

    /**
     * @param mixed $ms_sums
     *
     * @return self
     */
    public function set_ms_sums($ms_sums)
    {
        $this->ms_sums = $ms_sums;

        return $this;
    }

    public static function upload_file($file)
    {
        global $sql;


        $nameToSearch = trim(substr($_FILES['fileToUpload']['name'],0,-4));

        $kid = $sql->single_variable('SELECT uk_id FROM user_kid WHERE uk_name = "' . $nameToSearch . '";');

        if (!$kid) {
            return 1; //nem talalhato gyerek
        }

        $sql->execute_query('DELETE milestone_training FROM milestone_training JOIN milestone ON ms_training_mst_id = mst_id WHERE ms_user_kid_uk_id = ' . $kid);
        $sql->execute_query('DELETE milestone_competition FROM milestone_competition JOIN milestone ON ms_competition_msc_id = msc_id WHERE ms_user_kid_uk_id = ' . $kid);
        $sql->execute_query('DELETE milestone_camp FROM milestone_camp JOIN milestone ON ms_camp_msca_id= msca_id WHERE ms_user_kid_uk_id = ' . $kid);
        $sql->execute_query('DELETE milestone_ranking FROM milestone_ranking JOIN milestone ON ms_ranking_msr_id= msr_id WHERE ms_user_kid_uk_id = ' . $kid);
        $sql->execute_query('DELETE milestone_season FROM milestone_season JOIN milestone ON ms_season = mss_id WHERE ms_user_kid_uk_id = ' . $kid);


        $content = file($_FILES['fileToUpload']['tmp_name']); //makes array


        //var_dump($content);

        /*
            Szerkezet:

            s 2018-2019 --> szezon: ez megy a fejlécbe, ami lenyílik
            x edzés *
            y verseny *

            e 2018 10 --> edzés éééé-hh
            dátum F
            2 edzés F
            1 csoportos *
            1 külön *

            v --> verseny
            Dátum F
            Diákolimpia F
            Kiskunfélegyháza F
            Korcsoport F
            Helyezés F
            Mérkőzések: *
            Egyéni 1. hely *
            Páros 2. hely *

            t 2016-07 ---> tábor
            2018. július 17-24.
            Budapest, Hodos

            r 2018-09  ---> ranglista
            2018. szeptember - 39. hét
            felnőtt női egyéni: 28. hely
            felnőtt női páros: 24. hely
            felnőtt női vegyes páros: 23. hely
            U19 lány egyéni: 32. hely
            U19 lány páros: 20. hely
            U19 lány vegyes páros: 23. hely
            U17 lány egyéni: 18. hely
            U17 lány páros: 17. hely
            U17 lány vegyes páros: 12. hely

            A fájlt kötelező szezonnal kezdeni!

        */

        $milestoneId = null;
//var_dump($content);
        for ($lineNo = 0; $lineNo < count($content); ++$lineNo) {
            //var_dump($lineNo);
            $line = $content[$lineNo];
            //var_dump($line);

            //1. sor lekezelése: ha nem szezon, akkor hiba!
            if (0 === $lineNo && 's' != substr($line, 0, 1)) {
                return 3;
            }

            //ha nincs beállítva ms id, akkor új egység következik és beállítjuk
            if (null === $milestoneId) {
                $milestoneId = substr($line, 0, 1);
//var_dump($line);
//var_dump($milestoneId);
                //$ms = new milestone();
                //$mss_id = null;
                $mst_id = 'null';
                $msc_id = 'null';
                $msca_id = 'null';
                $msr_id = 'null';
                $sums = 'null';
                $type = 'null';
            }
//var_dump($milestoneId);
            switch ($milestoneId) {
                case 's':
                    # SZEZON
                    $mss_text = "";

                    $j = $lineNo;
                    while(isset($content[$j]) && !empty(trim($content[$j]))) {
                        //ha még csak a szezon első sorát olvassuk, akkor csak az "s" után rész kell
                        if ($j == $lineNo) {
                            //$content[$j] = substr($content[$j], 2);
                            $j++;
                            continue;
                        }
                        $mss_text .= trim($content[$j]) . '\n';
                        //++$lineNo;
                        $j++;
                    }
                    $lineNo = $j;
//var_dump($lineNo);
                    $mss_id = milestone_season::create_mss($mss_text);

                    break;
                case 'e':
                    # EDZÉS
                    $type = 1;

                    $mst = new milestone_training;

                    ++$lineNo;
                    $mst_date = trim($content[$lineNo]);
//var_dump('date: ' . $mst_date);

                    ++$lineNo;
                    $mst_count = trim($content[$lineNo]);

                    $j = $lineNo + 1;
                    $mst_trainings = "";
                    while(isset($content[$j]) && !empty(trim($content[$j]))) {
                        $mst_trainings .= trim($content[$j]) . '\n';
                        ++$j;
                    }
                    $lineNo = $j;
//var_dump($mst_date, $mst_count, $mst_trainings);
                    $mst_id = milestone_training::create_mst($mst_date, $mst_count, $mst_trainings);

                    break;

                case 't':
                    # TÁBOR
                    $type = 3;

                    ++$lineNo;
                    $msca_date = trim($content[$lineNo]);

                    ++$lineNo;
                    $msca_place = trim($content[$lineNo]);

                    $msca_id = milestone_camp::create_msca($msca_date, $msca_place);

                    break;

                case 'v':
                    # VERSENY
                    $type = 2;

                    ++$lineNo;
                    $msc_date = trim($content[$lineNo]);

                    ++$lineNo;
                    $msc_name = trim($content[$lineNo]);

                    ++$lineNo;
                    $msc_location = trim($content[$lineNo]);

                    ++$lineNo;
                    $msc_category = trim($content[$lineNo]);


                    ++$lineNo;
                    $msc_place = trim($content[$lineNo]);

                    ++$lineNo;
                    if (isset($content[$lineNo]) && !empty(trim($content[$lineNo]))) {
                        ++$lineNo; //mérkőzések szöveg
                        $matches = '';
                        do {
                            $matches .= trim($content[$lineNo]) . '\n';
                            ++$lineNo;
                        } while (isset($content[$lineNo]) && !empty(trim($content[$lineNo])));
                    }
                    else {
                        $matches = 'null';
                    }

                    $msc_id = milestone_competition::create_msc($msc_date, $msc_name, $msc_category, $msc_location, $msc_place, $matches);

                    break;

                case 'r':
                    # RANGLISTA

                    $type = 4;

                    //get season after 'r'
                    $msr_season = trim(substr($content[$lineNo], 2));

                    ++$lineNo;
                    $msr_date = trim($content[$lineNo]);

                    ++$lineNo;
                    $rankings = '';
                    do {
                        $rankings .= trim($content[$lineNo]) . '\n';
                        ++$lineNo;
                    } while (isset($content[$lineNo]) && !empty(trim($content[$lineNo])));

                    $msr_id = milestone_ranking::create_msr($msr_date, $rankings, $msr_season);

                    break;

                default:
                    # code...
                    break;
            }

            //todo: ezeket a változókat mindenképp be kell állítani h ne legyenek undefined-ok
            if ('null' != $type) {
                self::create_ms($type, $mss_id, $kid, $mst_id, $msc_id, $msca_id, $msr_id, $sums);
            }
            $milestoneId = null;
            //$lineNo++;


            //ha üres sor, akkor:
            //eddig adatok rögzítése az adatbázisban
            //ID nullozása



        } //endfor

        return 0;


    }
}


?>
