<?php

namespace App\Http\Controllers;

use App\Article;
use App\Feature;
use Illuminate\Http\Request;
use App\Fortuneteller;
use App\Area;
use App\Shop;
use App\Fortunemethod;

/**
 * Class HomeController
 *
 * @package App\Http\Controllers
 */
class SiteMapController extends Controller
{
    private $_root_url;
    private $_config_root;
    private $_config_url;
    private $_config_root_note;
    private $_config_url_note;

    public function __construct()
    {
        $this->_root_url = url('/');
        $uri = str_replace("https:", "", $this->_root_url);
        $uri = str_replace("http:", "", $uri);

        $this->_config_root = [
            'template'      => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $uri . '/main-sitemap.xsl"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>',
            'caseSensitive' => false,
            'charset'       => 'utf-8',
            'rowName'       => 'sitemap',
        ];
        $this->_config_url = [
            'template'      => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $uri . '/main-sitemap.xsl"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>',
            'caseSensitive' => false,
            'charset'       => 'utf-8',
            'rowName'       => 'url',
        ];

        $this->_config_root_note = [
            'template'      => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $uri . '/main-note-sitemap.xsl"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>',
            'caseSensitive' => false,
            'charset'       => 'utf-8',
            'rowName'       => 'sitemap',
        ];
        $this->_config_url_note = [
            'template'      => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $uri . '/main-note-sitemap.xsl"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>',
            'caseSensitive' => false,
            'charset'       => 'utf-8',
            'rowName'       => 'url',
        ];
    }

    public function index()
    {
        $data = [
            ['loc' => $this->_root_url . '/sitemap-page.xml', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-teller.xml', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-area.xml', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-store.xml', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-method.xml', 'lastmod' => '2020-12-17T09:54:20+09:00'],
        ];

        return response()->xml($data, 200, $this->_config_root);
    }

    public function page()
    {
        $data = [
            ['loc' => $this->_root_url . '/', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/contact', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/policy', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/support', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/401', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/404', 'lastmod' => '2020-12-17T09:54:20+09:00'],
        ];

        return response()->xml($data, 200, $this->_config_url);
    }

    public function teller()
    {
        $data = [['loc' => $this->_root_url . '/fortune-teller', 'lastmod' => '2020-12-17T09:54:20+09:00']];
        $tellers = Fortuneteller::select('id', 'slug', 'updated_at')->where('is_deleted', 0)->where('is_activated', 1)->get();
        foreach ($tellers as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->id;
            $time = '2020-12-17T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/fortune-teller/detail/' . $slug, 
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);
    }

    public function area()
    {
        $data = [];
        $tellers = Area::with('parent')->select('id', 'slug', 'parent_id', 'updated_at')->where('is_deleted', 0)->where('is_activated', 1)->get();
        foreach ($tellers as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->id;
            $time = '2020-12-17T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));
            $parentSlug = (isset($item->parent) && $item->parent->id != null) ? $item->parent->slug . '/' : '';

            $data[] = [
                'loc'       => $this->_root_url . '/area/' . $parentSlug . $slug,
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);
    }

    public function store()
    {
        $data = [['loc' => $this->_root_url . '/shop', 'lastmod' => '2020-12-17T09:54:20+09:00']];
        $tellers = Shop::select('id', 'slug', 'updated_at')->where('is_deleted', 0)->where('is_activated', 1)->get();
        foreach ($tellers as $item) {
            //$slug = $item->slug != "" ? $item->slug : $item->id;
            $time = '2020-12-17T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));
            
            $data[] = [
                'loc'       => $this->_root_url . '/shop/detail/' . $item->id, 
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);

    }

    public function method()
    {
        $data = [];
        $tellers = Fortunemethod::select('id', 'slug', 'updated_at')->where('is_deleted', 0)->where('is_activated', 1)->get();
        foreach ($tellers as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->id;
            $time = '2020-12-17T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/method/' . $slug, 
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);
    }

    public function noteSitemap()
    {
        $data = [
            ['loc' => $this->_root_url . '/note/sitemap-note-page.xml', 'lastmod' => '2021-06-08T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/note/sitemap-note-article.xml', 'lastmod' => '2021-06-08T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/note/sitemap-note-feature.xml', 'lastmod' => '2021-06-08T09:54:20+09:00'],
        ];

        return response()->xml($data, 200, $this->_config_root_note);
    }

    public function notePage() {
        $data = [
            ['loc' => $this->_root_url . '/note', 'lastmod' => '2021-06-08T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/note/article', 'lastmod' => '2021-06-08T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/note/feature', 'lastmod' => '2021-06-08T09:54:20+09:00'],
        ];

        return response()->xml($data, 200, $this->_config_url_note);
    }

    public function noteArticle()
    {
        $data = [];
        $tellers = Article::select('id', 'slug', 'updated_at')->where('is_deleted', 0)->where('is_activated', 1)->get();
        $time = '2021-06-08T09:54:20+09:00';
        foreach ($tellers as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->id;
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));
            $data[] = [
                'loc'       => $this->_root_url . '/note/article/' . $slug,
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url_note);
    }

    public function noteFeature()
    {
        $data = [];
        $features = Feature::select('id', 'slug', 'updated_at')->where('is_deleted', 0)->where('is_activated', 1)->get();
        $time = '2021-06-08T09:54:20+09:00';
        foreach ($features as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->id;
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));
            $data[] = [
                'loc'       => $this->_root_url . '/note/feature/' . $slug,
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url_note);
    }
}
