package handler

import (
	"bpmsapi/entity"
	"bpmsapi/repository"
	"bpmsapi/service"
	"bpmsapi/util"
	"fmt"
	"log"
	"time"

	// "fmt"
	"io"
	// "log"
	"net/http"
	// "time"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

type ServiceHandler struct {
	logName string
}

func NewServiceHandler(logName string) *ServiceHandler {
	return &ServiceHandler{
		logName: logName,
	}
}

func (serviceHandler *ServiceHandler) AddServiceHandler(c *gin.Context) {

	var response entity.CustomResponse
	db, err := util.GetDBConnection()
	if db != nil {
		defer db.Close()
	}

	if err != nil {
		response.Message = "Problem in Database connection. Please try again"
		response.Resultcode = 1
	}
	body := entity.AddServiceInput{}
	if err := c.ShouldBind(&body); err != nil && err != io.EOF {
		switch err.(type) {
		case validator.ValidationErrors:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.(validator.ValidationErrors).Translate(util.Trans),
			})
		default:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.Error()})

		}

		return
	} else {
		if err != nil && err == io.EOF {
			if util.CheckRequired(body) {
				c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Mandatory input fields missing"})
				return
			}
		}
	}
	body = util.Default_tag(body).(entity.AddServiceInput)
	var img_path_save1, img_path1 string
	if body.Service_Image_Small != nil {
		file, err1 := c.FormFile("service_image_small")

		if err1 != nil {
			log.Println(err1.Error())
			c.AbortWithError(http.StatusBadRequest, err1)
			return
		}
		if file == nil {
			errMsg := "No file uploaded for Service_Image_Small"
			c.JSON(http.StatusBadRequest, gin.H{"error": errMsg})
			return
		}
		timestamp := time.Now().Unix()
		imageName1 := fmt.Sprintf("%d%s", timestamp, file.Filename)
		// img_path1 = fmt.Sprintf("image/%s", imageName1)
		img_path1 = fmt.Sprintf("http://151.106.120.127/bashyam/assets/service_image/%s", imageName1)
		img_path_save1 = fmt.Sprintf("./var/www/html/bashyam/assets/service_image/%s", imageName1)

		if err2 := c.SaveUploadedFile(file, img_path_save1); err != nil {
			log.Println(err2.Error())
			c.AbortWithError(http.StatusBadRequest, err2)
			return
		}
	}
	var img_path2, img_path_save2 string
	if body.Service_Image_large != nil {
		file, err1 := c.FormFile("service_image_large")

		if err1 != nil {
			log.Println(err1.Error())
			c.AbortWithError(http.StatusBadRequest, err1)
			return
		}
		if file == nil {
			errMsg := "No file uploaded for Service_Image_large"
			c.JSON(http.StatusBadRequest, gin.H{"error": errMsg})
			return
		}
		timestamp := time.Now().Unix()
		imageName2 := fmt.Sprintf("%d%s", timestamp, file.Filename)
		// img_path2 = fmt.Sprintf("image/%s", imageName2)

		img_path2 = fmt.Sprintf("http://151.106.120.127/bashyam/assets/service_image/%s", imageName2)
		img_path_save2 = fmt.Sprintf("./var/www/html/bashyam/assets/service_image/%s", imageName2)

		if err2 := c.SaveUploadedFile(file, img_path_save2); err != nil {
			log.Println(err2.Error())
			c.AbortWithError(http.StatusBadRequest, err2)
			return
		}
	}
	srvRepository := repository.ServiceRepositoryInit(db)
	srvService := service.SrvServiceInit(srvRepository)
	response = srvService.AddSrvService(body, img_path1, img_path2)

	c.Set("response", response)

}

func (serviceRequestHandler *ServiceRequestHandler) UpdateProblemCategoryHandler(c *gin.Context) {

	var response entity.CustomResponse

	db, err := util.GetDBConnection()
	if db != nil {
		defer db.Close()
	}
	if err != nil {
		response.Message = "Problem in Database connection. Please try again"
		response.Resultcode = 1
		c.Set("response", response)
		return
	}
	body := entity.UpdateProblemCategoryInput{}
	if err := c.ShouldBindJSON(&body); err != nil && err != io.EOF {
		switch err.(type) {
		case validator.ValidationErrors:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.(validator.ValidationErrors).Translate(util.Trans),
			})
		default:
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
				"error": err.Error()})
		}

		return

	} else {
		if err != nil && err == io.EOF {
			if util.CheckRequired(body) {
				c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Mandatory input fields missing"})
				return
			}
		}
	}

	body = util.Default_tag(body).(entity.UpdateProblemCategoryInput)
	// tx, _ := db.Beginx()
	serviceRepository := repository.ServiceRepositoryInit(db)
	srqService := service.SrqServiceInit(nil, serviceRepository, nil)
	response = srqService.UpdateProblemCategory(body)
	c.Set("response", response)
}
