package util

import (
	"bpmsapi/entity"
	"encoding/json"
	"log"
	"net/http"
	"net/url"
)

const (
	SIGNUP int = iota
	OTP
	ADD_WALLET_AMOUNT
	NEW
	ASSIGNED
	IN_PROGRESS
	FIXED
	FURTHER_ACTION
	UNDER_OBSERVATION
	CLOSED
	CANCEL
)

func getTemplate(templateid int, payload []string) string {
	templates := map[int]string{
		OTP: "Hi Customer, Your OTP is " + payload[0] + ". This is valid only for 15 minutes and can be used only once. See you Soon, - FGIL Hygeia",
	}
	return templates[templateid]
}

func SendNotification(templateid int, payload []string, mobno string, emailid string, onechannelind int, dynmessage string) (reponse string) {
	var message string
	if templateid != -1 {
		message = getTemplate(templateid, payload)
	} else {
		message = dynmessage
	}
	var config *entity.Config
	config, _ = LoadConfiguration()
	if onechannelind == 1 {
		return sendSMSNotification(message, mobno, config.Notifications.Apikey, config.Notifications.Header, config.Notifications.Smsurl)
	} else {
		return ""
	}

}
func sendSMSNotification(message string, mobno string, apikey string, header string, smsurl string) (response string) {

	data := url.Values{
		"apiKey":  {apikey},
		"numbers": {mobno},
		"sender":  {header},
		"message": {message},
	}

	resp, err := http.PostForm(smsurl, data)
	if err != nil {
		log.Fatal(err)
	}
	var res map[string]interface{}
	json.NewDecoder(resp.Body).Decode(&res)
	if res["status"] == "success" {
		return "success"
	}else {
		return "failed"
	}
}

// func callPushNotification(templateid int, senderid string, payload []string, srqid int) (response string) {

// 	type notificationdata struct {
// 		Body string `json:"body"`
// 		Title string `json:"title"`
// 	}

// 	type notificationpayload struct {
// 		Srq_Id int `json:"srqid"`
// 	}

// 	type pnotificationdata struct {
// 		To string `json:"to"`
// 		Notification notificationdata `json:"notification"`
// 		Data notificationpayload `json:"data"`
// 	}
// 	var notifyData notificationdata
// 	var comdata pnotificationdata

// 	message := getTemplate(templateid, payload)


// }