<?php

namespace Modules\User\Facades;

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Modules\User\Models\User;

class Calendar
{

    /**
     * Notes   : 获取日历展示图,设置每周开始为周日
     *
     * @Date   : 2022/1/21 10:23
     * @Author : Mr.wang
     * @param  string  $date
     * @return array
     */
    public static function show(string $date = ''): array
    {
        $start = Carbon::parse($date)->startOfMonth()->startOfWeek(0)->toDateString();
        $end   = Carbon::parse($date)->endOfMonth()->endOfWeek(6)->toDateString();
        $data  = self::getBetweenDate($start, $end);

        $calendar = Carbon::parse($date);

        $days = $data->map(function ($today) use ($calendar) {
            return [
                'today'   => $today->toDateString(),
                'day'     => (int) $today->format('j'),
                'isPast'  => $today->isPast(),//是否为之前的日期
                'isMonth' => $today->isSameMonth($calendar),//当前月
            ];
        });

        return [
            'start'    => $start,
            'end'      => $end,
            'calendar' => $days
        ];

    }

    /**
     * Notes: 返回日期区间
     *
     * @Author: 玄尘
     * @Date: 2022/2/8 16:52
     * @param  string  $start_at  开始日期
     * @param  string  $end_at  结束日期
     */
    public static function getBetweenDate(string $start_at, string $end_at)
    {
        return collect(CarbonPeriod::create($start_at, $end_at));
    }

    /**
     * Notes: 获取日期 某一天的前一天后一天
     *
     * @Author: 玄尘
     * @Date: 2022/1/21 11:03
     * @param  string  $today
     * @param  string  $type
     * @return string
     */
    public static function day(string $today = '', string $type = ''): string
    {
        $variate = ['before' => -1, 'after' => 1, '' => 0][$type];

        return Carbon::parse($today)->addDays($variate)->toDateString();
    }

    /**
     * Notes: 获取日期 某一天的前一天后一天
     *
     * @Author: 玄尘
     * @Date: 2022/1/21 11:03
     * @param  string  $month
     * @param  string  $type
     * @return string
     */
    public static function getFirstDayByMonth(string $month = '', string $type = ''): string
    {
        $month = Carbon::parse($month);
        if ($type == 'before') {
            $month = $month->subMonth();
        }

        if ($type == 'after') {
            $month = $month->addMonth();
        }

        return $month->startOfMonth()->toDateString();
    }

    /**
     * Notes: 获取年份
     *
     * @Author: 玄尘
     * @Date: 2022/8/10 9:30
     * @param $start
     */
    public static function getYears($startYear)
    {
        $years = CarbonPeriod::years($startYear);
        return $years;
    }
}