<?php

namespace com\tyme\rabbyung;

use com\tyme\AbstractTyme;
use com\tyme\culture\Zodiac;
use InvalidArgumentException;

/**
 * 藏历月
 * @author 6tail
 * @package com\tyme\rabbyung
 */
class RabByungMonth extends AbstractTyme
{
    static array $NAMES = ['正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];

    /**
     * @var string[] 别名
     */
    static array $ALIAS = ['神变月', '苦行月', '具香月', '萨嘎月', '作净月', '明净月', '具醉月', '具贤月', '天降月', '持众月', '庄严月', '满意月'];

    /**
     * @var ?array 缓存{string:int[]}
     */
    protected static ?array $DAYS = null;

    /**
     * @var RabByungYear 藏历年
     */
    protected RabByungYear $year;

    /**
     * @var int 月
     */
    protected int $month;

    /**
     * @var bool 是否闰月
     */
    protected bool $leap;

    /**
     * @var int 位于当年的索引，0-12
     */
    protected int $indexInYear;

    private static function init(): void
    {
        $days = array();
        $y = 1950;
        $m = 11;
        $years = explode(',', '2c>,182[>1:2TA4ZI=n1E2Bk1J2Ff3Mk503Oc62g=,172^>1:2XA1>2UE2Bo1I2Fj3Lo62Fb3Mf5,03N^72b=1:2]A1>2ZF1B2VI2Em1K2Fe,2Lh1R3Na603P\\:172Y>1;2UB2=m2Dq1J2Eh,2Kl1Q3Me603Pa:172^>1;2YA2=p1C2UI,2Dk2Jp3QEc3Mi603Pf:3L[72b?1:2]A1<2UB2XH,2Cn1I2Ei1L2Ie1Q3Na703Q\\:2`@1;2XA,4\\H;m1B2TI2Em1L2Ij1Q3Nf603Q`903QW:,2[@1;2TB2XI1E4TMAh2Io3RFe3Mj603Pc803Q[;,2^?1;2WA2>q1E2Bm1I2Fi1M2Hc3Of70,3P^82a>1:2[A1>2WE1B2TI2Fm1L2Hf3Ni6,03Oa703PZ:3`A62V>4]F;q1B4YJ>l2Eq1L2Gi3Ml5,03Nd603Q_9172[>1;2XB2>p1E2VK2Fl,1K2Fc3Mh603Pc9172`>1;2\\B1>2UD2=j2En,1J2Fg3Mm62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?l1G4UOCh2Jl1Q2Le3Rh:,152`;172Z>1;2WB2@q1G2Cl2Ip1P2K_');
        foreach ($years as $ys) {
            while ($ys !== '') {
                $len = ord(substr($ys, 0, 1)) - 48;
                $data = array();
                for ($i = 0; $i < $len; $i++) {
                    $data[] = ord(substr($ys, $i + 1, 1)) - 83;
                }
                $days['' . ($y * 13 + $m)] = $data;
                $m++;
                $ys = substr($ys, $len + 1);
            }
            $y++;
            $m = 0;
        }
        static::$DAYS = $days;
    }

    function __construct(RabByungYear $year, int $month)
    {
        if (null == static::$DAYS) {
            static::init();
        }
        if ($month == 0 || $month > 12 || $month < -12) {
            throw new InvalidArgumentException(sprintf('illegal rab-byung month: %d', $month));
        }
        $y = $year->getYear();
        if ($y < 1950 || $y > 2050) {
            throw new InvalidArgumentException(sprintf('rab-byung year %d must between 1950 and 2050', $y));
        }
        $m = abs($month);
        if ($y == 1950 && $m < 12) {
            throw new InvalidArgumentException(sprintf('month %d must be 12 in rab-byung year %d', $month, $y));
        }
        $this->leap = $month < 0;
        $leapMonth = $year->getLeapMonth();
        if ($this->leap && $m != $leapMonth) {
            throw new InvalidArgumentException(printf('illegal leap month %d in rab-byung year %d', $m, $y));
        }
        $this->year = $year;
        $this->month = $m;
        $this->indexInYear = $m - 1 + ($this->leap || (0 < $leapMonth && $leapMonth < $m) ? 1 : 0);
    }

    static function fromYm(int $year, int $month): static
    {
        return new static(RabByungYear::fromYear($year), $month);
    }

    static function fromElementZodiac(int $rabByungIndex, RabByungElement $element, Zodiac $zodiac, int $month): static
    {
        return new static(RabByungYear::fromElementZodiac($rabByungIndex, $element, $zodiac), $month);
    }

    /**
     * 藏历年
     *
     * @return RabByungYear 藏历年
     */
    function getRabByungYear(): RabByungYear
    {
        return $this->year;
    }

    /**
     * 年
     *
     * @return int 年
     */
    function getYear(): int
    {
        return $this->year->getYear();
    }

    /**
     * 月
     *
     * @return int 月
     */
    function getMonth(): int
    {
        return $this->month;
    }

    /**
     * 月
     *
     * @return int 月，当月为闰月时，返回负数
     */
    function getMonthWithLeap(): int
    {
        return $this->leap ? -$this->month : $this->month;
    }

    /**
     * 位于当年的索引(0-12)
     *
     * @return int 索引
     */
    function getIndexInYear(): int
    {
        return $this->indexInYear;
    }

    /**
     * 是否闰月
     *
     * @return bool true/false
     */
    function isLeap(): bool
    {
        return $this->leap;
    }

    function getName(): string
    {
        return ($this->leap ? '闰' : '') . static::$NAMES[$this->month - 1];
    }

    /**
     * 别名
     *
     * @return string 别名
     */
    function getAlias(): string
    {
        return ($this->leap ? '闰' : '') . static::$ALIAS[$this->month - 1];
    }

    function __toString(): string
    {
        return $this->year . $this->getName();
    }

    function next($n): static
    {
        if ($n == 0) {
            return static::fromYm($this->getYear(), $this->getMonthWithLeap());
        }
        $m = $this->indexInYear + 1 + $n;
        $y = $this->year;
        if ($n > 0) {
            $monthCount = $y->getMonthCount();
            while ($m > $monthCount) {
                $m -= $monthCount;
                $y = $y->next(1);
                $monthCount = $y->getMonthCount();
            }
        } else {
            while ($m <= 0) {
                $y = $y->next(-1);
                $m += $y->getMonthCount();
            }
        }
        $leap = false;
        $leapMonth = $y->getLeapMonth();
        if ($leapMonth > 0) {
            if ($m == $leapMonth + 1) {
                $leap = true;
            }
            if ($m > $leapMonth) {
                $m--;
            }
        }
        return static::fromYm($y->getYear(), $leap ? -$m : $m);
    }

    /**
     * 首日
     *
     * @return RabByungDay 藏历日
     */
    function getFirstDay(): RabByungDay
    {
        return new RabByungDay($this, 1);
    }

    /**
     * 干支日列表
     *
     * @return RabByungDay[] 干支日列表
     */
    function getDays(): array
    {
        $days = [];
        $missDays = $this->getMissDays();
        $leapDays = $this->getLeapDays();
        for ($i = 1; $i <= 30; $i++) {
            if (in_array($i, $missDays)) continue;
            $days[] = new RabByungDay($this, $i);
            if (in_array($i, $leapDays)) {
                $days[] = new RabByungDay($this, -$i);
            }
        }
        return $days;
    }

    /**
     * 当月天数
     *
     * @return int 数量
     */
    function getDayCount(): int
    {
        return 30 + count($this->getLeapDays()) - count($this->getMissDays());
    }

    /**
     * 特殊日子列表，闰日为正，缺日为负
     *
     * @return int[] 特殊日子列表
     */
    function getSpecialDays(): array
    {
        $key = '' . ($this->getYear() * 13 + $this->indexInYear);
        return static::$DAYS[$key] ?? [];
    }

    /**
     * 闰日列表
     *
     * @return int[] 闰日列表
     */
    function getLeapDays(): array
    {
        return array_filter($this->getSpecialDays(), function ($d) {
            return $d > 0;
        });
    }

    /**
     * 缺日列表
     *
     * @return int[] 缺日列表
     */
    function getMissDays(): array
    {
        return array_map(function ($d) {
            return -$d;
        }, array_filter($this->getSpecialDays(), function ($d) {
            return $d < 0;
        }));
    }
}
