package parser

import (
	"RuleIniter/conf"
	"fmt"
)

type Parser struct {
	*conf.ParserConf
}

func New(c *conf.ParserConf) *Parser {
	return &Parser{
		ParserConf: c,
	}
}

func (p *Parser) Parse(failed map[string][]string) error {
	if fails, ok := failed["global"]; ok {
		ip, err := getIpv4Info()
		if err != nil {
			return fmt.Errorf("get ip info error: %s", err)
		}
		cu := ""
		if _, ok := failed[ip.Country]; ok {
			cu = ip.Country
		} else {
			for k := range failed {
				cu = k
				break
			}
		}
		failed[cu] = append(failed[cu], fails...)
		delete(failed, "global")
	}
	if p.EnableRoute {
		err := p.ParseRouteConfig(failed)
		if err != nil {
			return err
		}
	}
	if p.EnableDns {
		err := p.ParseDnsConfig(failed)
		if err != nil {
			return err
		}
	}
	return nil
}
