<?php

namespace App\Repositories;

use App\Libraries\ContractPDF;
use App\Models\File;
use App\User;
use Request;
use Response;
use Storage;
use Util;
use Validator;





class ContractPDFRepository extends BaseRepository {




    protected $disk;
    protected $dir;
    protected $user_id;
    protected $identifier;

    public function __construct(User $model){

        $this->model = $model;
    }

    public function _set($name, $value){
        $this->{$name} = $value;
    }

    private function convert($str){
        return iconv('UTF-8', 'windows-1252', $str);
    }

    public function createContractPDF() {

        $pdf = new ContractPDF();

        $pdf->AddPage('P', array(210, 297));
        $pdf->SetFont('Helvetica', '', 11);
        $pdf->SetDrawColor(160, 160, 160);

        $x1 = 16.5;
        $x2 = 109;
        $y = 70;
        $nl = 17.5;
        $pdf->SetXY($x1, $y);
        $pdf->Write(0, $this->convert($this->model->account->m_account));
        $pdf->SetXY($x2, $y);
        $pdf->Write(0, now()->format("d.m.Y"));

        $y += $nl;
        $pdf->SetXY($x1, $y);
        $pdf->Write(0, $this->convert($this->model->account->company));
        $pdf->SetXY($x2, $y);
        $pre = $this->model->account->pre_phone_id != "" ? $this->convert($this->model->account->pre_phone->phone)." " : "";
        $pdf->Write(0, $pre.$this->convert($this->model->account->phone));



        $y += $nl;
        $pdf->SetXY($x1, $y);
        $pdf->Write(0, $this->convert($this->model->account->m_first_name));
        $pdf->SetXY($x2, $y);
        $pre = $this->model->account->pre_mobil_id != "" ? $this->convert($this->model->account->pre_mobil->phone)." " : "";
        $pdf->Write(0, $pre.$this->convert($this->model->account->mobil));

        $y += $nl;
        $pdf->SetXY($x1, $y);
        $pdf->Write(0, $this->convert($this->model->account->m_last_name));
        $pdf->SetXY($x2, $y);
        $pdf->Write(0, $this->convert($this->model->email));

        $y += $nl;
        $pdf->SetXY($x1, $y);
        $pdf->Write(0, $this->convert($this->model->account->address));
        $pdf->SetXY($x2, $y);
        $pdf->Write(0, $this->convert($this->model->account->birthday));

        $y += $nl;
        $pdf->SetXY($x1, $y);
        $pdf->Write(0, $this->convert($this->model->account->zipcode)." ".$this->convert($this->model->account->city));

        $y += $nl;
        $pdf->SetXY($x1, $y);
        $pre = $this->model->account->country_id ? $this->convert($this->model->account->country->de)." " : "";
        $pdf->Write(0, $pre);

        if($this->model->m_sponsor){

            $y += 48;
            $pdf->SetXY($x1, $y);
            $pdf->Write(0, $this->convert($this->model->user_sponsor->account->company));
            $pdf->SetXY($x2, $y);
            $pdf->Write(0, $this->convert($this->model->user_sponsor->account->m_account));

            $y += $nl;
            $pdf->SetXY($x1, $y);
            $pdf->Write(0, $this->convert($this->model->user_sponsor->account->m_first_name)." ".$this->convert($this->model->user_sponsor->account->m_last_name));

            $website = "www.gruene-seele.bio";

        }else{
            $website = "www.gruene-seele.bio";
        }

        $pdf->AddPage('P', array(210, 297));
        $pdf->SetFont('Helvetica', '', 10);
        $pdf->SetDrawColor(160, 160, 160);
        $pdf->SetXY(52, 56);
        $pdf->Write(0, $website);


        $pdf->SetXY($x1, 65);
        $pdf->Write(0, $this->convert($this->model->account->m_first_name)." ".$this->convert($this->model->account->m_last_name));
        $pdf->SetXY($x2, 65);
        $pdf->Write(0, $this->convert($this->model->account->m_account));

        $pdf->AddPage('P', array(210, 297));


        if(!Storage::disk($this->disk)->exists( $this->dir )){
            Storage::disk($this->disk)->makeDirectory($this->dir); //creates directory
        }
        $filename = "Vertriebspartnervertrag.pdf";
        Storage::disk($this->disk)->put($this->dir.$filename, $pdf->Output('S'));
        $size =  Storage::disk($this->disk)->size($this->dir.$filename);
        $mine = Storage::disk($this->disk)->getMimeType($this->dir.$filename);

        File::create([
            'user_id' => $this->model->id,
            'identifier' => $this->identifier,
            'filename' => $filename,
            'dir' => $this->dir,
            'original_name' => $filename,
            'ext' => "pdf",
            'mine' => $mine,
            'size' => $size
        ]);

        return true;

    }
}