package parser

import (
	"encoding/json"
)

type Routing struct {
	RouteVersion   string          `json:"routeVersion,omitempty"`
	DomainStrategy string          `json:"domainStrategy,omitempty"`
	DomainMatcher  string          `json:"domainMatcher,omitempty"`
	Rules          []Rule          `json:"rules,omitempty"`
	Balancers      json.RawMessage `json:"balancers,omitempty"`
}

func (r Routing) Version() string {
	return r.RouteVersion
}

type Rule struct {
	DomainMatcher string          `json:"domainMatcher,omitempty"`
	Type          string          `json:"type,omitempty"`
	Domain        []string        `json:"domain,omitempty"`
	Ip            []string        `json:"ip,omitempty"`
	Port          string          `json:"port,omitempty"`
	SourcePort    string          `json:"sourcePort,omitempty"`
	Network       string          `json:"network,omitempty"`
	Source        json.RawMessage `json:"source,omitempty"`
	User          json.RawMessage `json:"user,omitempty"`
	InboundTag    json.RawMessage `json:"inboundTag,omitempty"`
	Protocol      json.RawMessage `json:"protocol,omitempty"`
	Attrs         string          `json:"attrs,omitempty"`
	OutboundTag   string          `json:"outboundTag,omitempty"`
	BalancerTag   string          `json:"balancerTag,omitempty"`
}

func (p *Parser) getDomainAndIpRules(region string, failed []string) (ds []string, ips []string) {
	if r, ok := p.MatchRuleList[region]; ok {
		return r.Domain, r.Ip
	}
	for _, m := range failed {
		ds = append(ds, p.MatchRuleList[m].Domain...)
		ips = append(ips, p.MatchRuleList[m].Ip...)
	}
	return
}

func isRuleChanged(r *Rule, domains []string, ips []string) bool {
	if isStringSliceNotEqual(r.Domain, domains) {
		return true
	}
	if isStringSliceNotEqual(r.Ip, ips) {
		return true
	}
	return false
}
