package vtiger

import (
	"crm-api/api"
	"crm-api/pkg/lfshook"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/gin-gonic/gin"
	"gopkg.in/ini.v1"
)

// const VTIGER_URL = "https://zycoo1.od2.vtiger.com"

// // Params 队列参数
// type CallInitiatedParams struct {
// 	From      string `form:"from"`
// 	To        string `form:"to"`
// 	Event     string `form:"event"`
// 	CallId    string `form:"call_id"`
// 	Direction string `form:"direction"`
// }

// @tags PBX-vtiger
// @Summary 查询contact
// @Description 查询contact
// @Security ApiKeyAuth
// @Accept  json
// @Produce  json
// @Router /api/vtiger/lookup [get]
func contactsInfo(ctx *gin.Context) {
	fmt.Printf("contactsInfo ............\n")

	// 获取 vtigerUrl
	// confPath := "/etc/asterisk/vtiger_api.conf"
	confPath := "/etc/asterisk/pms_api.conf"
	cfg, err := ini.Load(confPath)
	if err != nil {
		lfshook.NewLogger().Error(err)
		return
	}
	VtigerUrl := cfg.Section("general").Key("vtigerUrl").String()
	BasicAuthUser := cfg.Section("general").Key("vtigerBasicAuthUser").String()
	BasicAuthPWD := cfg.Section("general").Key("vtigerBasicAuthPWD").String()
	if VtigerUrl == "" || BasicAuthUser == "" || BasicAuthPWD == "" {
		lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set vtigerUrl or vtigerBasicAuthUser or vtigerBasicAuthPWD")
		return
	}

	// 获取 phone_number
	var reqVO CheckExistReqVO
	if err := ctx.ShouldBind(&reqVO); err != nil {
		api.Error(ctx, http.StatusBadRequest, err.Error())
		return
	}

	// 创建HTTP客户端
	client := &http.Client{}

	// 创建请求
	fmt.Printf("phone_number = %s\n", reqVO.PhoneNumber)
	// getURL := fmt.Sprintf("%s/modules/PhoneCalls/callbacks/Search.php?phone_number=%s", VTIGER_URL, reqVO.PhoneNumber)
	// getURL := fmt.Sprintf("%s/restapi/v1/vtiger/default/lookup?type=phone&value=%s", VTIGER_URL, reqVO.PhoneNumber)
	getURL := fmt.Sprintf("%s/restapi/v1/vtiger/default/lookup?type=phone&value=%s", VtigerUrl, reqVO.PhoneNumber)
	fmt.Printf("getURL = %s\n", getURL)
	req, err := http.NewRequest("GET", getURL, nil)
	if err != nil {
		fmt.Println("创建请求时发生错误:", err)
		return
	}
	// req.SetBasicAuth("juncheng.du@zycoo.com", "8DJ3O28MCZ4sPAk5")
	req.SetBasicAuth(BasicAuthUser, BasicAuthPWD)

	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("发送请求时发生错误:", err)
		return
	}
	defer resp.Body.Close()

	// 读取请求后的响应
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("读取请求后的响应时发生错误:", err)
		return
	}

	// 打印请求后的响应
	fmt.Printf("data = %+v\n", string(data))
	api.Success(ctx, string(data))
}

/* ===移到ami中了====================================================================================================
// @tags PBX-vtiger
// @Summary 呼叫发起事件
// @Description 呼叫发起事件
// @Security ApiKeyAuth
// @Accept  json
// @Produce  json
// @Router /api/vtiger/call-initiated [get]
func callInitiated(ctx *gin.Context) {
	fmt.Printf("callInitiated ............\n")
	var callInitiated CallInitiatedParams
	if err := ctx.ShouldBind(&callInitiated); err != nil {
		api.Error(ctx, http.StatusBadRequest, err.Error())
		return
	}

	fmt.Printf("From = %s\n", callInitiated.From)
	fmt.Printf("To = %s\n", callInitiated.To)
	fmt.Printf("Event = %s\n", callInitiated.Event)
	fmt.Printf("CallId = %s\n", callInitiated.CallId)
	fmt.Printf("Direction = %s\n", callInitiated.Direction)

	apiKeyValue := "174781790673da476e25cf"

	// 创建HTTP客户端
	client := &http.Client{}

	// 创建请求
	// https://zycoo1.od2.vtiger.com/modules/PhoneCalls/callbacks/Generic.php?from=10000002&to=12300001&event=call_initiated&call_id=12345678&direction=inbound
	getURL := fmt.Sprintf("%s/modules/PhoneCalls/callbacks/Generic.php?from=%s&to=%s&event=%s&call_id=%s&direction=%s", VTIGER_URL, callInitiated.From, callInitiated.To, callInitiated.Event, callInitiated.CallId, callInitiated.Direction)
	fmt.Printf("getURL = %s\n", getURL)
	req, err := http.NewRequest("GET", getURL, nil)
	if err != nil {
		fmt.Println("创建请求时发生错误:", err)
		return
	}
	req.Header.Set("X-VTIGER-SECRET", apiKeyValue)

	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("发送请求时发生错误:", err)
		return
	}
	defer resp.Body.Close()

	// 读取请求后的响应
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("读取请求后的响应后的响应时发生错误:", err)
		return
	}

	// 打印请求后的响应
	fmt.Printf("data = %+v\n", string(data))
	api.Success(ctx, string(data))
}
// ======================================================================================================= */
