<?php

namespace App\Http\Controllers;

use App\Http\Controllers\TMDB;
use Illuminate\Support\Arr;

class SitemapController extends Controller
{
    public function index()
    {
        return response()->view('sitemaps.index')->header('Content-Type', 'text/xml');
    }

    public function moviePopular()
    {
        $items = $this->getMovieTv('movie/popular');

        return response()->view('sitemaps.movie', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function movieTopRated()
    {
        $items = $this->getMovieTv('movie/top_rated');
        return response()->view('sitemaps.movie', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function movieUpcoming()
    {
        $items = $this->getMovieTv('movie/upcoming');
        return response()->view('sitemaps.movie', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function movieNowPlaying()
    {
        $items = $this->getMovieTv('movie/now_playing');
        return response()->view('sitemaps.movie', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function tvPopular()
    {
        $items = $this->getMovieTv('tv/popular');
        return response()->view('sitemaps.tv', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function tvTopRated()
    {
        $items = $this->getMovieTv('tv/top_rated');
        return response()->view('sitemaps.tv', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function tvOnTheAir()
    {
        $items = $this->getMovieTv('tv/on_the_air');
        return response()->view('sitemaps.tv', compact('items'))->header('Content-Type', 'text/xml');
    }

    public function tvAiringToday()
    {
        $items = $this->getMovieTv('tv/airing_today');
        return response()->view('sitemaps.tv', compact('items'))->header('Content-Type', 'text/xml');
    }

    private function getMovieTv($action)
    {
        $movies = [];
        for ($i=1; $i <= max_limit_page_sitemap(); $i++) {
            $tmdb = new TMDB($i);
            $movies[] = $tmdb->getMovieTv($action);
        }

        $datas = [];
        foreach ($movies as $data) {
            $datas[] = $data->results;
        }

        return Arr::collapse($datas);
    }
}
