<?php

namespace App\Http\Controllers\Admin;

use App\Template;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TemplateController extends Controller
{
    public function __construct()
    {
        if ( alreadyInstalled() ) {
            $this->middleware('auth');
        }
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $templates = Template::paginate();

        return view('templates.index', compact('templates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('templates.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Template::create($request->except('_token'));

        return redirect()->route('admin.template.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Template  $template
     * @return \Illuminate\Http\Response
     */
    public function edit(Template $template)
    {
        return view('templates.edit', compact('template'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Template  $template
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Template $template)
    {
        $template->touch();
        $template->update($request->except('_token'));

        return redirect()->route('admin.template.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Template  $template
     * @return \Illuminate\Http\Response
     */
    public function destroy(Template $template)
    {
        $template->delete();
        return redirect()->route('admin.template.index');
    }

    public function progress(String $string, Array $data)
    {
        $replace = $this->replace_shortcode($string, $data);
        return $this->spinProcess($replace);
    }

    public function spinProcess($text)
    {
        return preg_replace_callback(
            '/\{(((?>[^\{\}]+)|(?R))*?)\}/x',
            array($this, 'spinReplace'),
            $text
        );
    }

    public function spinReplace($text)
    {
        $text = $this->spinProcess($text[1]);
        $parts = explode('|', $text);

        return $parts[array_rand($parts)];
    }

    public function replace_shortcode(string $string, array $datas)
    {
        $content = preg_replace_callback('/\[(.*?)\]/', function($matches) use ($datas) {
            if ($matches[1] == 'year') {
                if (! empty($datas['release_date']) || isset($datas['release_date'])) {
                    return explode('-', $datas['release_date'])[0];
                }

                return explode('-', $datas['first_air_date'])[0];
            }
            if ($matches[1] == 'poster_path') {
                return 'https://image.tmdb.org/t/p/original'.$datas['poster_path'];
            }
            if ($matches[1] == 'backdrop_path') {
                return 'https://image.tmdb.org/t/p/original'.$datas['backdrop_path'];
            }
            if ($matches[1] == 'title') {
                return isset($datas['title']) ? $datas['title'] : $datas['name'];
            }
            if ($matches[1] == 'original_title') {
                return isset($datas['original_title']) ? $datas['original_title'] : $datas['original_name'];
            }
            if ($matches[1] == 'release_date') {
                return isset($datas['release_date']) ? $datas['release_date'] : $datas['first_air_date'];
            }
            if (isset($datas[$matches[1]])) {
                return $datas[$matches[1]];
            }

            return;
        }, $string);

        return $content;
    }
}
