<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Http\Client;

/**
 * Jiras Controller
 *
 * @property \App\Model\Table\JirasTable $Jiras
 *
 * @method \App\Model\Entity\Jira[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class JirasController extends AppController
{
    public function beforeFilter(Event $event) {
        //$this->Auth->setConfig('checkAuthIn', 'Controller.initialize');
        $this->Auth->allow(['index','add','edit','show']);
    }
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null
     */
    public function show($id = null)
        {
            $jira = $this->Jiras->get($id);
            //debug($jira);
            $http = new Client();
            //$tickets = ['423','469','533','568','421','424','569','422','586','587','588','589','514','420','590','591'];
            $tickets = explode(',',$jira->tickets);
            //debug($tickets);die;
            $tabledata = array();
            //$j = 0;
            foreach($tickets as $ticket) {
                //debug($ticket);

                $response = $http->get('https://kmjira.bayern.de/rest/api/latest/issue/' . $ticket .'?fields=description', [], 
                [
                    'proxy' => ['proxy' => 'www.proxy.bybn.de:80'],
                    'auth' => ['type' => 'basic', 'username' => 'TrinklW', 'password' => 'HiegTzs!12']
                ]);
                //debug($response->getStringBody());

                $data = json_decode($response->getStringBody());

                $skuList = explode(PHP_EOL, $data->fields->description);
                
                $istable = 0;
                $entries = array();
                $i=0;
                foreach($skuList as $line) {
                    if(substr($line,0,1) == '|' and substr($line,1,1) !== '|') {
                        $istable = 1;
                        //debug($i);
                        $entries[$i] = $line;
                    }
                    if($istable == 1 and substr($line,0,1) !== '|') {
                        //debug($i);
                        $entries[$i - 1] .= trim($line);
                    }
                    else if($istable == 1) {
                        $i++; 
                    }
                }
                
                foreach($entries as $entry) {
                    $entry = trim(preg_replace('/\s+/', ' ', $entry));
                    //debug($entry);
                    //debug(explode('|',$entry));
                    array_push($tabledata,explode('|',$entry));
                    //$tabledate[$j][] = explode('|',$entry);
                    //debug($j);
                    //debug($tabledate[$j]);
                    //debug($tabledata);
                    //$j++;
                }                
            }
            //debug($tabledata);
            $this->set('tabledata',$tabledata);
        }

    public function index()
    {
        $jiras = $this->paginate($this->Jiras);

        $this->set(compact('jiras'));
    }

    /**
     * View method
     *
     * @param string|null $id Jira id.
     * @return \Cake\Http\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $jira = $this->Jiras->get($id, [
            'contain' => []
        ]);

        $this->set('jira', $jira);
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $jira = $this->Jiras->newEntity();
        if ($this->request->is('post')) {
            $jira = $this->Jiras->patchEntity($jira, $this->request->getData());
            if ($this->Jiras->save($jira)) {
                $this->Flash->success(__('The jira has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The jira could not be saved. Please, try again.'));
        }
        $this->set(compact('jira'));
    }

    /**
     * Edit method
     *
     * @param string|null $id Jira id.
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $jira = $this->Jiras->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $jira = $this->Jiras->patchEntity($jira, $this->request->getData());
            if ($this->Jiras->save($jira)) {
                $this->Flash->success(__('The jira has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The jira could not be saved. Please, try again.'));
        }
        $this->set(compact('jira'));
    }

    /**
     * Delete method
     *
     * @param string|null $id Jira id.
     * @return \Cake\Http\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $jira = $this->Jiras->get($id);
        if ($this->Jiras->delete($jira)) {
            $this->Flash->success(__('The jira has been deleted.'));
        } else {
            $this->Flash->error(__('The jira could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
