package trenditapi

import (
	"errors"
	"fmt"
)

//增加打印机
func (a *API) AddPrinter(sn, key, name string) error {
	params := []AddPrinterReq{{
		Sn:   sn,
		Key:  key,
		Name: name,
	}}
	resp, err := a.HttpPostJson("addPrinter", params)
	if err != nil {
		return err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return fmt.Errorf("添加打印机错误: %v", resp.BaseRes.Message)
	}
	retVal, _ := resp.BaseRes.Data.(map[string]interface{})
	failMsg := ""
	if len(retVal["fail"].([]interface{})) > 0 {
		for _, v := range retVal["fail"].([]interface{}) {
			t := v.(FailItem)
			failMsg += t.Sn + ":" + t.Reason + " "
		}
		return fmt.Errorf("添加打印机错误: %v", failMsg)
	}
	return nil
}

//修改打印机信息
func (a *API) EditPrinter(sn, name string) (string, error) {
	resp, err := a.HttpPostJson("editPrinter", []EditPrinterReq{{
		Sn:   sn,
		Name: name,
	},
	})
	if err != nil {
		return "", err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return "", errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return "", fmt.Errorf("修改打印机信息错误：%v", resp.BaseRes.Message)
	}
	retVal, _ := resp.BaseRes.Data.(map[string]interface{})
	failMsg := ""
	if len(retVal["fail"].([]interface{})) > 0 {
		for _, v := range retVal["fail"].([]interface{}) {
			t := v.(FailItem)
			failMsg += t.Sn + ":" + t.Reason + " "
		}
		return fmt.Sprintf("修改打印机信息错误：%v", failMsg), nil
	}
	return "", nil
}

//删除打印机
func (a *API) DelPrinter(snList []string) error {
	resp, err := a.HttpPostJson("delPrinter", snList)
	if err != nil {
		return err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return fmt.Errorf("删除打印机错误：%v", resp.BaseRes.Message)
	}
	retVal, _ := resp.BaseRes.Data.(map[string]interface{})
	failMsg := ""
	if len(retVal["fail"].([]interface{})) > 0 {
		for _, v := range retVal["fail"].([]interface{}) {
			t := v.(FailItem)
			failMsg += t.Sn + ":" + t.Reason + " "
		}
		return fmt.Errorf("删除打印机错误：%v", failMsg)
	}
	return nil
}

//设置打印机浓度
func (a *API) SetDensity(sn string, density int) error {
	resp, err := a.HttpPostJson("setDensity", SetDensityReq{
		Sn:      sn,
		Density: density,
	})
	if err != nil {
		return err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return fmt.Errorf("设置打印机浓度错误：%v", resp.BaseRes.Message)
	}
	return nil
}

//设置音量
func (a *API) SetVolume(sn string, volume int) error {
	resp, err := a.HttpPostJson("setVolume", SetVolumeReq{
		Sn:     sn,
		Volume: volume,
	})
	if err != nil {
		return err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return fmt.Errorf("设置打印机音量错误：%v", resp.BaseRes.Message)
	}
	return nil
}

//查询打印机状态
func (a *API) GetDeviceStatus(sn string) (float64, float64, error) {
	resp, err := a.HttpPostJson("getDeviceStatus", GetDeviceStatusReq{Sn: sn})
	if err != nil {
		return 0, 0, err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return 0, 0, errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return 0, 0, fmt.Errorf("查询打印机状态错误：%v", resp.BaseRes.Message)
	}
	retVal, _ := resp.BaseRes.Data.(map[string]interface{})
	return retVal["onlineStatus"].(float64), retVal["workStatus"].(float64), nil
}

//清空设备待打印队列
func (a *API) CleanWaitingQueue(sn string) error {
	resp, err := a.HttpPostJson("cleanWaitingQueue", sn)
	if err != nil {
		return err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return fmt.Errorf("清空设备待打印队列错误：%v", resp.BaseRes.Message)
	}
	return nil
}

//打印小票
func (a *API) Print(sn, content, voice string) (string, error) {
	param := PrintReq{
		Sn:                sn,
		Content:           content,
		Voice:             voice,
		VoicePlayTimes:    1,
		VoicePlayInterval: 3,
		Copies:            1,
	}
	//if sn == "580011097147" { // 这个门店要三次
	//	param.VoicePlayTimes = 3
	//}
	resp, err := a.HttpPostJson("print", param)
	if err != nil {
		return "", err
	}
	if resp.HttpStatusCode != HttpStatusSuccessCode {
		return "", errors.New("HTTP请求错误，请检查重试")
	}
	if resp.BaseRes.Code != ResponseCodeSuccess {
		return "", fmt.Errorf("打印错误：%v", resp.BaseRes.Message)
	}
	retVal := resp.BaseRes.Data.(map[string]interface{})
	if retVal["printId"].(string) != "" {
		return retVal["printId"].(string), nil
	}
	return "", nil
}
