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

import (
	"context"

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

func createVinsInAcc(ctx context.Context, d *schema.ResourceData, m interface{}, accID uint64) (vins.CreateInAccountRequest, diag.Diagnostics) {
	c := m.(*controller.ControllerCfg)
	req := vins.CreateInAccountRequest{}

	if diags := checkParamsExistenceCreateInAcc(ctx, d, c); diags != nil {
		return req, diags
	}

	req.AccountID = accID
	req.Name = d.Get("name").(string)

	if gid, ok := d.GetOk("gid"); ok {
		req.GID = uint64(gid.(int))
	}
	if IPCIDR, ok := d.GetOk("ipcidr"); ok {
		req.IPCIDR = IPCIDR.(string)
	}
	if description, ok := d.GetOk("description"); ok {
		req.Description = description.(string)
	}
	if preReservationsNum, ok := d.GetOk("pre_reservations_num"); ok {
		req.PreReservationsNum = uint64(preReservationsNum.(int))
	}

	if routesList, ok := d.GetOk("routes"); ok {
		var routes []vins.Route
		var route vins.Route
		for _, r := range routesList.([]map[string]interface{}) {
			route.Netmask = r["netmask"].(string)
			route.Destination = r["destination"].(string)
			route.Gateway = r["gateway"].(string)
			routes = append(routes, route)
		}
		req.Routes = routes
	}
	if dns, ok := d.GetOk("dns"); ok {
		dnsInterface := dns.(*schema.Set).List()
		req.DNSList = make([]string, 0, len(dnsInterface))
		for _, item := range dnsInterface {
			req.DNSList = append(req.DNSList, item.(string))
		}
	}

	if zoneID, ok := d.GetOk("zone_id"); ok {
		req.ZoneID = uint64(zoneID.(int))
	}

	req.EnableSecGroups = d.Get("enable_secgroups").(bool)

	return req, nil
}

func createVinsInRG(ctx context.Context, d *schema.ResourceData, m interface{}, RGID uint64) (vins.CreateInRGRequest, diag.Diagnostics) {
	c := m.(*controller.ControllerCfg)
	req := vins.CreateInRGRequest{}

	if diags := checkParamsExistenceCreateInRG(ctx, d, c); diags != nil {
		return req, diags
	}

	req.RGID = RGID
	req.Name = d.Get("name").(string)

	if IPCIDR, ok := d.GetOk("ipcidr"); ok {
		req.IPCIDR = IPCIDR.(string)
	}

	if extNetID, ok := d.GetOk("ext_net_id"); ok {
		req.ExtNetID = int64(extNetID.(int))
	}

	if extIP, ok := d.GetOk("ext_ip"); ok {
		req.ExtIP = extIP.(string)
	}
	if description, ok := d.GetOk("description"); ok {
		req.Description = description.(string)
	}
	if preReservationsNum, ok := d.GetOk("pre_reservations_num"); ok {
		req.PreReservationsNum = uint64(preReservationsNum.(int))
	}

	if routesList, ok := d.GetOk("routes"); ok {
		var routes []vins.Route
		var route vins.Route
		for _, r := range routesList.([]map[string]interface{}) {
			route.Netmask = r["netmask"].(string)
			route.Destination = r["destination"].(string)
			route.Gateway = r["gateway"].(string)
			routes = append(routes, route)
		}
		req.Routes = routes
	}
	if dns, ok := d.GetOk("dns"); ok {
		dnsInterface := dns.(*schema.Set).List()
		req.DNSList = make([]string, 0, len(dnsInterface))
		for _, item := range dnsInterface {
			req.DNSList = append(req.DNSList, item.(string))
		}
	}

	if zoneID, ok := d.GetOk("zone_id"); ok {
		req.ZoneID = uint64(zoneID.(int))
	}

	req.EnableSecGroups = d.Get("enable_secgroups").(bool)

	return req, nil
}

func isContainsIp(els []interface{}, el interface{}) bool {
	elConv := el.(map[string]interface{})
	for _, elOld := range els {
		elOldConv := elOld.(map[string]interface{})
		if elOldConv["ip_addr"].(string) == elConv["ip_addr"].(string) &&
			elOldConv["type"].(string) == elConv["type"].(string) &&
			elOldConv["mac"].(string) == elConv["mac"].(string) &&
			elOldConv["compute_id"].(string) == elConv["compute_id"].(string) {
			return true
		}
	}
	return false
}

func isContainsNatRule(els []interface{}, el interface{}) bool {
	elConv := el.(map[string]interface{})
	for _, elOld := range els {
		elOldConv := elOld.(map[string]interface{})
		if elOldConv["int_ip"].(string) == elConv["int_ip"].(string) &&
			elOldConv["int_port"].(int) == elConv["int_port"].(int) &&
			elOldConv["ext_port_start"].(int) == elConv["ext_port_start"].(int) {
			return true
		}
	}
	return false
}
