package app

import (
	"crm-api/api/admin/zoho"
	"crm-api/internal/app/ami"
	httpServer "crm-api/internal/app/http_server"
	"crm-api/internal/app/mysql"
	"crm-api/internal/app/redis"

	socketio "crm-api/internal/app/socket_io"
	"crm-api/internal/app/status"
	"fmt"
	"sync"

	"crm-api/pkg/lfshook"

	"gopkg.in/ini.v1"
)

// var once sync.Once
var once sync.Once // 20241128 vtiger crm 对应

func StartApp() {
	mysql.CreateDBInstance()
	redis.CreateRedisInstance()
	socketio.StartSocketIO()
	// i18n.InitBundle()
	// initCronTask()

	go httpServer.StartRoute()
	/* 20230411 pms 删除 =======================================================================================================
	go ami.StartAMI(func() {
		lfshook.NewLogger().Info("ami callback function")
		// 首次连接才进行初始化
		once.Do(func() {
			status.InitAsterisk()
			// agi.InitPageingPlan(configs.ConfigGlobal.AsteriskPagingPath)
		})
	}, []func(event map[string]string){})
	* ========================================================================================================================= */

	// go agi.StartAGI()
	// go systeminfo.SendMessage() // 20230411 pms 删除

	// 启动插件程序
	// go plugin.StartUP() // 20230411 pms 删除

	// /* 20241128 vtiger crm 对应 =======================================================================================================
	CrmConfPath := "/etc/asterisk/crm_api.conf"
	crmCfg, crmErr := ini.Load(CrmConfPath)
	if crmErr != nil {
		lfshook.NewLogger().Error(crmErr)
		return
	}
	CrmEnabled := crmCfg.Section("general").Key("enable").String()
	ApiType := crmCfg.Section("general").Key("apiType").String()
	if CrmEnabled == "" || ApiType == "" {
		lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set enabled or apiType")
		return
	}

	/* **************************************************************************
	// 先暂时都使用一个配置文件 pms_api.conf
	VtigerConfPath := "/etc/asterisk/vtiger_api.conf"
	vtigerCfg, vtigerErr := ini.Load(VtigerConfPath)
	if vtigerErr != nil {
		lfshook.NewLogger().Error(vtigerErr)
		return
	}
	VtigerEnabled := vtigerCfg.Section("general").Key("enabled").String()
	if VtigerEnabled == "" {
		lfshook.NewLogger().Error("/etc/asterisk/vtiger_api.conf not set enabled")
		return
	}

	if VtigerEnabled == "yes" && PmsEnabled == "no" { // pms基本是数据库操作，但是有个叫醒，会产生事件，所以当 pms 开启时，不能开启 vtiger
	// ************************************************************************** */
	fmt.Println("ApiType = ", ApiType)
	// if ApiType == "CRM_vtiger" || ApiType == "CRM_zoho" {  // 把 crm 和 pms 分开了
	if CrmEnabled == "yes" {
		if ApiType == "CRM_zoho" {
			fmt.Println("Ticker ...")
			// 每隔55分钟刷新token
			/* ************************************************************************** 定时器放在这里没有用
			ticker := time.NewTicker(1 * time.Minute)
			defer ticker.Stop()
			done := make(chan bool)
			go func() {
				for {
					select {
					case <-done:
						return
					case t := <-ticker.C:
						fmt.Println("Tick at", t)
						zoho.RefreshToken()
					}
				}
			}()
			// ************************************************************************** */
			go zoho.RefreshTokenTicker()
			go zoho.RefreshToken()
			go zoho.ZohoUserExtenList()
			go zoho.RefreshZohoUserExtenListTicker()
		}
		fmt.Println("ami.StartAMI ...")
		go ami.StartAMI(func() {
			lfshook.NewLogger().Info("ami callback function")
			// 首次连接才进行初始化
			once.Do(func() {
				status.InitAsterisk()
				// agi.InitPageingPlan(configs.ConfigGlobal.AsteriskPagingPath)
			})
		}, []func(event map[string]string){}, ApiType)
	}

	// * ========================================================================================================================= */
}
