/*
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
Tim Tkachev, <tvtkachev@basistech.ru>

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 kvmvm

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/dpdknet"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/extnet"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/image"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/rg"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/trunk"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/vfpool"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/vins"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func existRgID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
	c := m.(*controller.ControllerCfg)
	rgId := uint64(d.Get("rg_id").(int))
	req := rg.ListRequest{
		IncludeDeleted: false,
	}

	rgList, err := c.CloudAPI().RG().List(ctx, req)
	if err != nil {
		return false, err
	}

	return len(rgList.FilterByID(rgId).Data) != 0, nil
}

func existImageId(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
	c := m.(*controller.ControllerCfg)
	imageId := uint64(d.Get("image_id").(int))
	req := image.ListRequest{
		ByID: imageId,
	}

	imageList, err := c.CloudAPI().Image().List(ctx, req)
	if err != nil {
		return false, err
	}

	return len(imageList.Data) != 0, nil
}

func existVinsIdInList(vinsId uint64, vinsList *vins.ListVINS) bool {
	for _, vins := range vinsList.Data {
		if vinsId == vins.ID {
			return true
		}
	}

	return false
}

func existExtNetIdInList(extId uint64, extList *extnet.ListExtNets) bool {
	for _, ext := range extList.Data {
		if extId == ext.ID {
			return true
		}
	}

	return false
}

func existVinsId(ctx context.Context, m interface{}, id int) (int, bool) {
	c := m.(*controller.ControllerCfg)
	req := vins.ListRequest{IncludeDeleted: false}

	vinsList, err := c.CloudAPI().VINS().List(ctx, req)
	if err != nil {
		log.Debugf("Unable to retrieve ViNS list, %s", err)
		return 0, false
	}

	if !existVinsIdInList(uint64(id), vinsList) {
		return id, false
	}

	return 0, true
}

func existExtNetId(ctx context.Context, m interface{}, id int) (int, bool) {
	c := m.(*controller.ControllerCfg)
	req := extnet.ListRequest{}

	extNetList, err := c.CloudAPI().ExtNet().List(ctx, req)
	if err != nil {
		log.Debugf("Unable to retrieve extnet list, %s", err)
		return 0, false
	}

	if !existExtNetIdInList(uint64(id), extNetList) {
		return id, false
	}

	return 0, true
}

func existVFPoolId(ctx context.Context, m interface{}, id int) (int, bool) {
	c := m.(*controller.ControllerCfg)
	req := vfpool.ListRequest{ByID: uint64(id)}

	vfpoolList, err := c.CloudAPI().VFPool().List(ctx, req)
	if err != nil {
		log.Debugf("Unable to retrieve vfpool list, %s", err)
		return id, false
	}

	if len(vfpoolList.Data) == 1 {
		return 0, true
	}

	return id, false
}

func existDPDKNetId(ctx context.Context, m interface{}, id int) (int, bool) {
	c := m.(*controller.ControllerCfg)
	req := dpdknet.ListRequest{ByID: uint64(id)}

	dpdkList, err := c.CloudAPI().DPDKNet().List(ctx, req)
	if err != nil {
		log.Debugf("Unable to retrieve dpdk list, %s", err)
		return id, false
	}

	if len(dpdkList.Data) == 1 {
		return 0, true
	}

	return id, false
}

func existTRUNKId(ctx context.Context, m interface{}, id int) (int, bool) {
	c := m.(*controller.ControllerCfg)
	req := trunk.ListRequest{IDs: []uint64{uint64(id)}}

	trunkList, err := c.CloudAPI().Trunk().List(ctx, req)
	if err != nil {
		log.Debugf("Unable to retrieve trunk list, %s", err)
		return id, false
	}

	if len(trunkList.Data) == 1 {
		return 0, true
	}

	return id, false
}
