package tyme

import (
	"fmt"
	"strings"
	"sync"
)

var RabByungMonthNames = []string{"正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"}
var RabByungMonthAliases = []string{"神变月", "苦行月", "具香月", "萨嘎月", "作净月", "明净月", "具醉月", "具贤月", "天降月", "持众月", "庄严月", "满意月"}
var RabByungMonthDays = map[int][]int{}
var onceRabByungMonth sync.Once

// RabByungMonth 藏历月，仅支持藏历1950年十二月至藏历2050年十二月
type RabByungMonth struct {
	AbstractTyme
	// 藏历年
	year RabByungYear
	// 月
	month int
	// 是否闰月
	leap bool
	// 位于当年的索引，0-12
	indexInYear int
}

func NewRabByungMonth(year RabByungYear, month int) (*RabByungMonth, error) {
	onceRabByungMonth.Do(func() {
		RabByungMonthDays = make(map[int][]int)
		y := 1950
		m := 11
		years := strings.Split("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,1J2Fg3Mm62Ib3Pj;3M_703R[:2`B1=2YB2=n,1C2TI2Fk1L2Ig1P3Nd703Q_:152X<2[A,2<q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for _, ys := range years {
			for ys != "" {
				length := int(ys[0] - '0')
				data := make([]int, length)
				for i := 0; i < length; i++ {
					data[i] = int(ys[i+1]) - '5' - 30
				}
				key := y*13 + m
				RabByungMonthDays[key] = data
				m++
				ys = ys[length+1:]
			}
			y++
			m = 0
		}
	})
	if month == 0 || month > 12 || month < -12 {
		return nil, fmt.Errorf("illegal rab-byung month: %d", month)
	}
	y := year.GetYear()
	if y < 1950 || y > 2050 {
		return nil, fmt.Errorf("rab-byung year %d must between 1950 and 2050", y)
	}
	m := month
	if m < 0 {
		m = -m
	}
	if y == 1950 && m < 12 {
		return nil, fmt.Errorf("month %d must be 12 in rab-byung year %d", month, y)
	}
	leap := month < 0
	leapMonth := year.GetLeapMonth()
	if leap && m != leapMonth {
		return nil, fmt.Errorf("illegal leap month %d in rab-byung year %d", m, y)
	}
	index := m - 1
	if leap || (0 < leapMonth && leapMonth < m) {
		index++
	}
	return &RabByungMonth{
		year:        year,
		month:       m,
		leap:        leap,
		indexInYear: index,
	}, nil
}

func (RabByungMonth) FromYm(year, month int) (*RabByungMonth, error) {
	y, err := RabByungYear{}.FromYear(year)
	if err != nil {
		return nil, err
	}
	return NewRabByungMonth(*y, month)
}

func (RabByungMonth) FromElementZodiac(rabByungIndex int, element RabByungElement, zodiac Zodiac, month int) (*RabByungMonth, error) {
	y, err := RabByungYear{}.FromElementZodiac(rabByungIndex, element, zodiac)
	if err != nil {
		return nil, err
	}
	return NewRabByungMonth(*y, month)
}

func (o RabByungMonth) GetRabByungYear() RabByungYear {
	return o.year
}

func (o RabByungMonth) GetYear() int {
	return o.year.GetYear()
}

func (o RabByungMonth) GetMonth() int {
	return o.month
}

func (o RabByungMonth) GetMonthWithLeap() int {
	if o.leap {
		return -o.month
	}
	return o.month
}

func (o RabByungMonth) GetIndexInYear() int {
	return o.indexInYear
}

func (o RabByungMonth) IsLeap() bool {
	return o.leap
}

func (o RabByungMonth) GetName() string {
	name := RabByungMonthNames[o.month-1]
	if o.leap {
		return "闰" + name
	}
	return name
}

func (o RabByungMonth) GetAlias() string {
	alias := RabByungMonthAliases[o.month-1]
	if o.leap {
		return "闰" + alias
	}
	return alias
}

func (o RabByungMonth) String() string {
	return fmt.Sprintf("%s%s", o.year.GetName(), o.GetName())
}

func (o RabByungMonth) Next(n int) (*RabByungMonth, error) {
	if n == 0 {
		return NewRabByungMonth(o.year, o.GetMonthWithLeap())
	}
	m := o.indexInYear + 1 + n
	y := &o.year
	if n > 0 {
		monthCount := y.GetMonthCount()
		for m > monthCount {
			m -= monthCount
			ny, err := y.Next(1)
			if err != nil {
				return nil, err
			}
			y = ny
			monthCount = y.GetMonthCount()
		}
	} else {
		for m <= 0 {
			ny, err := y.Next(-1)
			if err != nil {
				return nil, err
			}
			y = ny
			m += y.GetMonthCount()
		}
	}

	leap := false
	leapMonth := y.GetLeapMonth()
	if leapMonth > 0 {
		if m == leapMonth+1 {
			leap = true
		}
		if m > leapMonth {
			m--
		}
	}
	if leap {
		m = -m
	}
	return NewRabByungMonth(*y, m)
}

func (o RabByungMonth) GetFirstDay() RabByungDay {
	d, _ := NewRabByungDay(o, 1)
	return *d
}

func (o RabByungMonth) GetDays() []RabByungDay {
	var days []RabByungDay
	missDays := o.GetMissDays()
	leapDays := o.GetLeapDays()
	missDayMap := make(map[int]bool)
	leapDayMap := make(map[int]bool)
	for _, d := range missDays {
		missDayMap[d] = true
	}
	for _, d := range leapDays {
		leapDayMap[d] = true
	}
	for i := 1; i <= 30; i++ {
		if _, exists := missDayMap[i]; exists {
			continue
		}
		d, _ := NewRabByungDay(o, i)
		days = append(days, *d)
		if _, exists := leapDayMap[i]; exists {
			ld, _ := NewRabByungDay(o, -i)
			days = append(days, *ld)
		}
	}
	return days
}

func (o RabByungMonth) GetDayCount() int {
	return 30 + len(o.GetLeapDays()) - len(o.GetMissDays())
}

func (o RabByungMonth) GetSpecialDays() []int {
	key := o.GetYear()*13 + o.GetIndexInYear()
	days, exists := RabByungMonthDays[key]
	if !exists {
		return []int{}
	}
	return days
}

func (o RabByungMonth) GetLeapDays() []int {
	var days []int
	for _, d := range o.GetSpecialDays() {
		if d > 0 {
			days = append(days, d)
		}
	}
	return days
}

func (o RabByungMonth) GetMissDays() []int {
	var days []int
	for _, d := range o.GetSpecialDays() {
		if d < 0 {
			days = append(days, -d)
		}
	}
	return days
}

func (o RabByungMonth) Equals(target RabByungMonth) bool {
	return o.String() == target.String()
}
