//error system package
package icserror

import (
	"errors"
	"fmt"
	"runtime"
	"strings"
)

type IcsError struct {
	code    interface{}
	message string
	err     error
}

func GetModName() string {
	return "icserror"
}

func NewIcsError(errString string, code interface{}) *IcsError {
	var err IcsError
	err.err = errors.New(errString)
	err.code = code
	err.message = errString

	return &err
}

func (e IcsError) Print() {
	fmt.Printf("Code: %v, Message: %s, Error: %v\n", e.code, e.message, e.err)
}

func (e IcsError) PrintWithCaller(depth int) {
	errstr := fmt.Sprintf("Code: %v, Message: %s", e.code, e.message)
	funcname, file, line, ok := runtime.Caller(depth)
	if ok {
		files := strings.Split(file, "/")
		fileslen := len(files)
		func1 := runtime.FuncForPC(funcname).Name()
		funcs := strings.Split(func1, "/")
		funcslen := len(funcs)

		fmt.Printf("[%s:%d %s] %s\n", files[fileslen-1], line, funcs[funcslen-1], errstr)
	}
}

func (e IcsError) Equal(i *IcsError) bool {
	return e.code == i.code
}

func (e *IcsError) SetError(baseError error) {
	e.err = baseError
}

func (e *IcsError) GetCode() interface{} {
	return e.code
}

func (e *IcsError) GetMessage() string {
	return e.message
}

func (e *IcsError) GetError() error {
	return e.err
}