/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tag

import (
	"strings"
	"sync"
)

import (
	"github.com/dubbogo/gost/log/logger"

	"gopkg.in/yaml.v2"
)

import (
	"dubbo.apache.org/dubbo-go/v3/common"
	conf "dubbo.apache.org/dubbo-go/v3/common/config"
	"dubbo.apache.org/dubbo-go/v3/common/constant"
	"dubbo.apache.org/dubbo-go/v3/config"
	"dubbo.apache.org/dubbo-go/v3/config_center"
	"dubbo.apache.org/dubbo-go/v3/protocol/base"
	"dubbo.apache.org/dubbo-go/v3/remoting"
)

type PriorityRouter struct {
	routerConfigs sync.Map
}

func NewTagPriorityRouter() (*PriorityRouter, error) {
	return &PriorityRouter{}, nil
}

// Route Determine the target invokers list.
func (p *PriorityRouter) Route(invokers []base.Invoker, url *common.URL, invocation base.Invocation) []base.Invoker {
	if len(invokers) == 0 {
		logger.Warnf("[tag router] invokers from previous router is empty")
		return invokers
	}
	// tag is valid in application
	key := strings.Join([]string{url.GetParam(constant.ApplicationKey, ""), constant.TagRouterRuleSuffix}, "")
	value, ok := p.routerConfigs.Load(key)
	if !ok {
		return staticTag(invokers, url, invocation)
	}
	routerCfg := value.(config.RouterConfig)
	if !*routerCfg.Enabled || !*routerCfg.Valid {
		return staticTag(invokers, url, invocation)
	}
	return dynamicTag(invokers, url, invocation, routerCfg)
}

func (p *PriorityRouter) URL() *common.URL {
	return nil
}

func (p *PriorityRouter) Priority() int64 {
	return 0
}

func (p *PriorityRouter) Notify(invokers []base.Invoker) {
	if len(invokers) == 0 {
		return
	}
	application := invokers[0].GetURL().GetParam(constant.ApplicationKey, "")
	if application == "" {
		logger.Error("url application is empty")
		return
	}
	dynamicConfiguration := conf.GetEnvInstance().GetDynamicConfiguration()
	if dynamicConfiguration == nil {
		logger.Infof("Config center does not start, Tag router will not be enabled")
		return
	}
	key := strings.Join([]string{application, constant.TagRouterRuleSuffix}, "")
	dynamicConfiguration.AddListener(key, p)
	value, err := dynamicConfiguration.GetRule(key)
	if err != nil {
		logger.Errorf("query router rule fail,key=%s,err=%v", key, err)
		return
	}
	if value == "" {
		logger.Infof("router rule is empty,key=%s", key)
		return
	}
	p.Process(&config_center.ConfigChangeEvent{Key: key, Value: value, ConfigType: remoting.EventTypeAdd})
}

func (p *PriorityRouter) Process(event *config_center.ConfigChangeEvent) {
	if event.ConfigType == remoting.EventTypeDel {
		p.routerConfigs.Delete(event.Key)
		return
	}
	routerConfig, err := parseRoute(event.Value.(string))
	if err != nil {
		logger.Warnf("[tag router]Parse new tag route config error, %+v "+
			"and we will use the original tag rule configuration.", err)
		return
	}
	p.routerConfigs.Store(event.Key, *routerConfig)
	logger.Infof("[tag router]Parse tag router config success,routerConfig=%+v", routerConfig)
}

func parseRoute(routeContent string) (*config.RouterConfig, error) {
	routeDecoder := yaml.NewDecoder(strings.NewReader(routeContent))
	routerConfig := &config.RouterConfig{}
	err := routeDecoder.Decode(routerConfig)
	if err != nil {
		return nil, err
	}
	routerConfig.Valid = new(bool)
	*routerConfig.Valid = true
	if len(routerConfig.Tags) == 0 {
		*routerConfig.Valid = false
	}
	return routerConfig, nil
}
