<?php

namespace App\Http\Controllers;

use App\Store;

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

    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',
        ];
    }

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

        $totalStore = Store::isPublished()->count();
        $totalPage = floor($totalStore/40);
        if ($totalStore % 40 > 0) $totalPage += 1;
        if ($totalPage <= 0) $totalPage = 1;
        for ($i = 1; $i <= $totalPage; $i++) {
            $data[] = ['loc' => $this->_root_url . '/sitemap-store-' . $i . '.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 . '/policy', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/register', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/help', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/line', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/list', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/store', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/company', 'lastmod' => '2020-12-17T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/kiyaku', '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 . '/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 store($page = 1)
    {
        $data = [];
        $stores = Store::select('id', 'slug', 'title')->isPublished()->paginate(40, ['*'], 'page', $page);
        foreach ($stores as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->title;
            $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 . '/store/' . $item->id . '/' . $slug,
                'lastmod' => $time
            ];
        }

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

}
