// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package audio

import (
	"encoding/json"
	"io/ioutil"
	"path/filepath"

	"github.com/linuxdeepin/go-lib/pulse"
	"github.com/linuxdeepin/go-lib/xdg/basedir"
)

// dconfig默认优先级配置
var (
	inputDefaultPriorities  = []int{} // 输入设备默认优先级
	outputDefaultPriorities = []int{} // 输出设备默认优先级
)

// PriorityManager 优先级管理器
// 管理音频设备的优先级策略，包含输入和输出设备的优先级配置
type PriorityManager struct {
	Output *PriorityPolicy // 输出设备优先级策略
	Input  *PriorityPolicy // 输入设备优先级策略

	file string // 配置文件的路径，私有成员不会被json导出
}

// NewPriorityManager 创建优先级管理器实例
// path: 配置文件路径
func NewPriorityManager(path string) *PriorityManager {
	return &PriorityManager{
		Output: NewPriorityPolicy(),
		Input:  NewPriorityPolicy(),
		file:   path,
	}
}

// createPriorityManagerSingleton 创建优先级管理器单例工厂函数
// path: 配置文件路径
func createPriorityManagerSingleton(path string) func() *PriorityManager {
	var pm *PriorityManager = nil
	return func() *PriorityManager {
		if pm == nil {
			pm = NewPriorityManager(path)
		}
		return pm
	}
}

// 获取优先级管理器单例
// 由于优先级管理需要在很多个对象中使用，放在Audio对象中需要添加额外参数传递到各个模块很不方便，因此在此创建一个全局的单例
var globalPrioritiesFilePath = filepath.Join(basedir.GetUserConfigDir(), "deepin/dde-daemon/priorities.json")
var GetPriorityManager = createPriorityManagerSingleton(globalPrioritiesFilePath)

// Print 打印优先级列表，用于调试
// 将当前优先级配置以JSON格式输出到日志
func (pm *PriorityManager) Print() {
	data, err := json.MarshalIndent(pm, "", "  ")
	if err != nil {
		logger.Warning(err)
		return
	}

	logger.Debugf("PriorityManager:\n %s", string(data))
}

// Save 保存配置文件
// 将当前优先级配置保存到JSON文件
func (pm *PriorityManager) Save() {
	data, err := json.MarshalIndent(pm, "", "  ")
	if err != nil {
		logger.Warning(err)
		return
	}

	err = ioutil.WriteFile(pm.file, data, 0644)
	if err != nil {
		logger.Warning(err)
		return
	}
}

// Load 读取配置文件
// 从JSON文件加载优先级配置
func (pm *PriorityManager) Load() bool {
	data, err := ioutil.ReadFile(pm.file)
	if err != nil {
		logger.Warningf("failed to read file '%s': %v", pm.file, err)
		return false
	}

	err = json.Unmarshal(data, pm)
	if err != nil {
		logger.Warningf("failed to parse json of file '%s': %v", pm.file, err)
		return false
	}

	return true
}

// completeDefaultTypes 加载dconfig默认优先级
// 补充默认的设备类型优先级配置
func (pm *PriorityManager) completeDefaultTypes() {
	for _, i := range inputDefaultPriorities {
		if !pm.Input.Types.hasElement(i) {
			pm.Input.Types = append(pm.Input.Types, i)
			logger.Debugf("input defualt append type %d", i)
		}
	}
	for _, i := range outputDefaultPriorities {
		if !pm.Output.Types.hasElement(i) {
			pm.Output.Types = append(pm.Output.Types, i)
			logger.Debugf("output defualt append type %d", i)
		}
	}
}

// defaultInit 使用默认值进行初始化
// cards: 声卡列表
func (pm *PriorityManager) defaultInit(cards CardList) {
	// 初始化类型优先级列表
	pm.completeDefaultTypes()
	pm.Output.completeTypes()
	pm.Input.completeTypes()

	// 添加可用的端口
	for _, card := range cards {
		for _, port := range card.Ports {
			if port.Available == pulse.AvailableTypeNo {
				logger.Debugf("unavailable port '%s(%s)' card:<%s> port:<%s>", port.Description, card.Name, card.core.Name, port.Name)
				continue
			}

			if port.Direction == pulse.DirectionSink {
				pm.Output.AddRawPort(card.core, &port)
			} else if port.Direction == pulse.DirectionSource {
				pm.Input.AddRawPort(card.core, &port)
			} else {
				logger.Warningf("unexpected direction %d of port <%s:%s>",
					port.Direction, card.core.Name, port.Name)
			}
		}
	}
}

// SetPorts 设置有效端口
// 这是因为从配置文件读出来的数据是上次运行时保存的，两次运行之间（例如关机状态下）可能插拔了设备
// 因此需要删除无效端口，添加新的有效端口
// cards: 声卡列表
func (pm *PriorityManager) SetPorts(cards CardList) {
	outputPorts := make(PriorityPortList, 0)
	inputPorts := make(PriorityPortList, 0)
	for _, card := range cards {
		for _, port := range card.Ports {
			if port.Available == pulse.AvailableTypeNo {
				logger.Debugf("unavailable port '%s(%s)' card:<%s> port:<%s>", port.Description, card.Name, card.core.Name, port.Name)
				continue
			}

			_, portConfig := GetConfigKeeper().GetCardAndPortConfig(card.core.Name, port.Name)
			if !portConfig.Enabled {
				logger.Debugf("diabled port '%s(%s)' card:<%s> port:<%s>", port.Description, card.Name, card.core.Name, port.Name)
				continue
			}

			p := PriorityPort{
				CardName: card.core.Name,
				PortName: port.Name,
				PortType: DetectPortType(card.core, &port),
			}

			if port.Direction == pulse.DirectionSink {
				outputPorts = append(outputPorts, &p)
				logger.Debugf("append output port %s:%s", p.CardName, p.PortName)
			} else {
				inputPorts = append(inputPorts, &p)
				logger.Debugf("append input port %s:%s", p.CardName, p.PortName)
			}
		}
	}
	pm.Output.SetPorts(outputPorts)
	pm.Input.SetPorts(inputPorts)
}

// Init 进行初始化
// cards: 声卡列表
func (pm *PriorityManager) Init(cards CardList) {
	if !pm.Load() {
		pm.defaultInit(cards)
		pm.Save()
		return
	}

	// 设置有效端口
	pm.SetPorts(cards)

	// 补充缺少的类型（产品修改了端口类型），并更新端口排序
	pm.completeDefaultTypes()
	pm.Output.completeTypes()
	pm.Output.sortPorts()
	pm.Input.completeTypes()
	pm.Input.sortPorts()

	pm.Save()
}

// SetTheFirstPort 设置优先级最高的端口（自动识别输入输出）
// card: 声卡
// port: 端口信息
func (pm *PriorityManager) SetTheFirstPort(card *pulse.Card, port *pulse.CardPortInfo) {
	if port.Direction == pulse.DirectionSink {
		pm.Output.SetTheFirstPort(card.Name, port.Name)
	} else if port.Direction == pulse.DirectionSource {
		pm.Input.SetTheFirstPort(card.Name, port.Name)
	} else {
		logger.Warningf("unexpected direction %d of port <%s:%s>",
			port.Direction, card.Name, port.Name)
	}

	// 打印并保存
	pm.Print()
	pm.Save()
}

// SetFirstOutputPort 设置优先级最高的输出端口
// cardName: 声卡名称
// portName: 端口名称
func (pm *PriorityManager) SetFirstOutputPort(cardName string, portName string) {
	pm.Output.SetTheFirstPort(cardName, portName)
	pm.Print()
	pm.Save()
}

// SetFirstInputPort 设置优先级最高的输入端口
// cardName: 声卡名称
// portName: 端口名称
func (pm *PriorityManager) SetFirstInputPort(cardName string, portName string) {
	pm.Input.SetTheFirstPort(cardName, portName)
	pm.Print()
	pm.Save()
}
