package sms

import (
	"fmt"
	"strconv"
)

func DecodeError(code int) string {
	switch code {
	case 300:
		return "ME failure"
	case 301:
		return "SMS service of ME reserved"
	case 302:
		return "Operation not allowed"
	case 303:
		return "Operation not supported"
	case 304:
		return "Invalid PDU mode parameter"
	case 305:
		return "Invalid text mode parameter"
	case 310:
		return "SIM not inserted"
	case 311:
		return "SIM PIN required"
	case 312:
		return "PH-SIM PIN required"
	case 313:
		return "SIM failure"
	case 314:
		return "SIM busy"
	case 315:
		return "SIM wrong"
	case 316:
		return "SIM PUK required"
	case 317:
		return "SIM PIN2 required"
	case 318:
		return "SIM PUK2 required"
	case 320:
		return "Memory failure"
	case 321:
		return "Invalid memory index"
	case 322:
		return "Memory full"
	case 330:
		return "SMSC address unknown"
	case 331:
		return "No network service"
	case 332:
		return "Network timeout"
	case 340:
		return "NO +CNMA ACK EXPECTED"
	case 341:
		return "Buffer overflow"
	case 342:
		return "SMS size more than expected"
	case 500:
		return "Unknown error"
	}
	return "UNDEFINED ERROR CODE"
}

func GetError(msg []byte) (int, error) {
	if len(msg) >= len("+CMS ERROR: ")+3 && string(msg[:len("+CMS ERROR: ")]) == "+CMS ERROR: " {
		return strconv.Atoi(string(msg[len("+CMS ERROR: ") : len("+CMS ERROR: ")+3]))
	}
	return 0, fmt.Errorf("failed to parse error")
}