/* Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved.
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// Package common a series of common function
package common

import (
	"strconv"

	"ascend-common/common-utils/hwlog"
)

type int32Tool struct {
}

type int64Tool struct {
}

type stringTool struct {
}

// Int32Tool slice for int32 tool
var Int32Tool int32Tool

// Int64Tool slice for int64 tool
var Int64Tool int64Tool

// StringTool slice for string tool
var StringTool stringTool

func Contains[T comparable](sources []T, target T) bool {
	for _, sourceNum := range sources {
		if sourceNum == target {
			return true
		}
	}
	return false
}

// Contains slice for int32 contains
func (i int32Tool) Contains(sources []int32, target int32) bool {
	for _, sourceNum := range sources {
		if sourceNum == target {
			return true
		}
	}
	return false
}

// SameElement slice for int64 has same element with others slice
func (i int64Tool) SameElement(sources, targets []int64) bool {
	for _, source := range sources {
		for _, target := range targets {
			if source == target {
				return true
			}
		}
	}
	return false
}

// Remove slice for int64 remove target
func (i int64Tool) Remove(sources []int64, target int64) []int64 {
	if len(sources) == 0 {
		return sources
	}
	index := i.Index(sources, target)
	if index == -1 {
		return sources
	}
	return i.Remove(append(sources[:index], sources[index+1:]...), target)
}

// Index slice for int64 search the index with target
func (i int64Tool) Index(sources []int64, target int64) int {
	for index, source := range sources {
		if source == target {
			return index
		}
	}
	return -1
}

// ToHexString slice for int64 to Hex string
func (i int64Tool) ToHexString(sources []int64) string {
	var target string
	for i, source := range sources {
		if i == 0 {
			target = FormatFaultCodeHex(source)
			continue
		}
		target = target + "," + FormatFaultCodeHex(source)
	}
	return target
}

// Abs return a absolute value between two int 64 value
func (i int64Tool) Abs(var1, var2 int64) int64 {
	if var1 > var2 {
		return var1 - var2
	}
	return var2 - var1
}

// Contains slice for int64 contains
func (i int64Tool) Contains(sources []int64, target int64) bool {
	for _, sourceNum := range sources {
		if sourceNum == target {
			return true
		}
	}
	return false
}

// HexStringToInt hex string slice to int64 slice
func (s stringTool) HexStringToInt(sources []string) []int64 {
	intSlice := make([]int64, 0, len(sources))
	for _, source := range sources {
		num, err := strconv.ParseInt(source, Hex, 0)
		if err != nil {
			hwlog.RunLog.Errorf("parse hex int failed and skip it, string: %s", source)
			continue
		}
		intSlice = append(intSlice, num)
	}
	return intSlice
}

// Contains check whether slice contains target
func (s stringTool) Contains(sources []string, target string) bool {
	for _, v := range sources {
		if v == target {
			return true
		}
	}
	return false
}