/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Sergey Kisil, <svkisil@digitalenergy.online>

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.
*/

/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.

Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort

Please see README.md to learn where to place source code so that it
builds seamlessly.

Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/

package stack

import (
	log "github.com/sirupsen/logrus"
	"strconv"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/stack"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)

func flattenStack(d *schema.ResourceData, st *stack.InfoStack) {
	log.Debugf("flattenStack: decoded Stack name %q / ID %d",
		st.Name, st.ID)

	d.Set("ckey", st.Ckey)
	d.Set("meta", flattens.FlattenMeta(st.Meta))
	d.Set("api_url", st.APIURL)
	d.Set("api_key", st.Apikey)
	d.Set("app_id", st.AppID)
	d.Set("cpu_allocation_ratio", st.CPUAllocationRatio)
	d.Set("description", st.Description)
	d.Set("descr", st.Descr)
	d.Set("drivers", st.Drivers)
	d.Set("eco", flattenEco(st.Eco))
	d.Set("error", st.Error)
	d.Set("gid", st.GID)
	d.Set("guid", st.GUID)
	d.Set("stack_id", st.ID)
	d.Set("images", st.Images)
	d.Set("login", st.Login)
	d.Set("mem_allocation_ratio", st.MemAllocationRatio)
	d.Set("name", st.Name)
	d.Set("packages", flattenPackages(st.Packages))
	d.Set("passwd", st.Password)
	d.Set("reference_id", st.ReferenceID)
	d.Set("status", st.Status)
	d.Set("type", st.Type)
}

func flattenPackages(pg stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"libvirt_bin":        flattenLibvirtBin(pg),
		"libvirt_daemon":     flattenLibvirtDaemon(pg),
		"lvm2_lockd":         flattenLvm2Lockd(pg),
		"openvswitch_common": flattenOpenvswitchCommon(pg),
		"openvswitch_switch": flattenOpenvswitchSwitch(pg),
		"qemu_system_x86":    flattenQemuSystemX86(pg),
		"sanlock":            flattenSanlock(pg),
	}
	res = append(res, temp)
	return res
}

func flattenLibvirtBin(lb stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": lb.LibvirtBin.InstalledSize,
		"ver":            lb.LibvirtBin.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenLibvirtDaemon(ld stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": ld.LibvirtDaemon.InstalledSize,
		"ver":            ld.LibvirtDaemon.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenLvm2Lockd(ll stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": ll.Lvm2Lockd.InstalledSize,
		"ver":            ll.Lvm2Lockd.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenOpenvswitchCommon(oc stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": oc.OpenvswitchCommon.InstalledSize,
		"ver":            oc.OpenvswitchCommon.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenOpenvswitchSwitch(os stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": os.OpenvswitchSwitch.InstalledSize,
		"ver":            os.OpenvswitchSwitch.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenQemuSystemX86(qs stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": qs.QemuSystemX86.InstalledSize,
		"ver":            qs.QemuSystemX86.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenSanlock(sl stack.Packages) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"installed_size": sl.Sanlock.InstalledSize,
		"ver":            sl.Sanlock.Ver,
	}
	res = append(res, temp)
	return res
}

func flattenEco(m interface{}) string {
	switch d := m.(type) {
	case string:
		return d
	case int:
		return strconv.Itoa(d)
	case int64:
		return strconv.FormatInt(d, 10)
	case float64:
		return strconv.FormatInt(int64(d), 10)
	default:
		return ""
	}
}

func flattenStacksList(sl *stack.ListStacks) []map[string]interface{} {
	res := make([]map[string]interface{}, 0, len(sl.Data))
	for _, item := range sl.Data {
		temp := map[string]interface{}{
			"ckey":                 item.Ckey,
			"meta":                 flattens.FlattenMeta(item.Meta),
			"api_url":              item.APIURL,
			"api_key":              item.Apikey,
			"app_id":               item.AppID,
			"cpu_allocation_ratio": item.CPUAllocationRatio,
			"description":          item.Description,
			"descr":                item.Descr,
			"drivers":              item.Drivers,
			"eco":                  flattenEco(item.Eco),
			"error":                item.Error,
			"gid":                  item.GID,
			"guid":                 item.GUID,
			"stack_id":             item.ID,
			"images":               item.Images,
			"login":                item.Login,
			"mem_allocation_ratio": item.MemAllocationRatio,
			"name":                 item.Name,
			"packages":             flattenPackages(item.Packages),
			"passwd":               item.Password,
			"reference_id":         item.ReferenceID,
			"status":               item.Status,
			"type":                 item.Type,
		}
		res = append(res, temp)
	}
	return res
}
