package accessgroup

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/sdn/acsgroups"
)

func flattenAccessGroupDataSource(d *schema.ResourceData, accessGroupRecord *acsgroups.AccessGroup) {
	d.Set("display_name", accessGroupRecord.DisplayName)
	d.Set("comment", accessGroupRecord.Comment)
	d.Set("created_at", accessGroupRecord.CreatedAt)
	d.Set("net_object_access_group", flattenNetObjectAccessGroup(accessGroupRecord.NetObjectAccessGroup))
}

func flattenAccessGroupResource(d *schema.ResourceData, accessGroupRecord *acsgroups.AccessGroup, userList *acsgroups.UsersList) {
	d.Set("display_name", accessGroupRecord.DisplayName)
	d.Set("comment", accessGroupRecord.Comment)
	d.Set("created_at", accessGroupRecord.CreatedAt)
	d.Set("net_object_access_group", flattenNetObjectAccessGroup(accessGroupRecord.NetObjectAccessGroup))

	defaultSecurityPolicy := accessGroupRecord.DefaultSecurityPolicy
	if defaultSecurityPolicy.ID != "" {
		flattenedDefaultSecPolicy := map[string]interface{}{
			"display_name":              defaultSecurityPolicy.DisplayName,
			"id":                        defaultSecurityPolicy.ID,
			"description":               defaultSecurityPolicy.Description,
			"version_id":                int(defaultSecurityPolicy.VersionID),
			"access_group_id":           defaultSecurityPolicy.AccessGroupID,
			"default_open_session_drop": defaultSecurityPolicy.DefaultOpenSessionDrop,
		}

		if defaultSecPolicy, ok := d.GetOk("default_security_policy"); ok {
			defaultSecPolicyList := defaultSecPolicy.([]interface{})
			if len(defaultSecPolicyList) > 0 {
				defaultSecPolicyMap := defaultSecPolicyList[0].(map[string]interface{})

				if defaultAclDrop, ok := defaultSecPolicyMap["default_acl_drop"].(string); ok && defaultAclDrop != "" {
					flattenedDefaultSecPolicy["default_acl_drop"] = defaultAclDrop
				} else if defaultSecurityPolicy.DefaultAclDrop != "" {
					flattenedDefaultSecPolicy["default_acl_drop"] = defaultSecurityPolicy.DefaultAclDrop
				}

				if defaultOpenSessionDrop, ok := defaultSecPolicyMap["default_open_session_drop"].(bool); ok {
					flattenedDefaultSecPolicy["default_open_session_drop"] = defaultOpenSessionDrop
				}
			} else {
				if defaultSecurityPolicy.DefaultAclDrop != "" {
					flattenedDefaultSecPolicy["default_acl_drop"] = defaultSecurityPolicy.DefaultAclDrop
				}
			}
		} else {
			if defaultSecurityPolicy.DefaultAclDrop != "" {
				flattenedDefaultSecPolicy["default_acl_drop"] = defaultSecurityPolicy.DefaultAclDrop
			}
		}

		d.Set("default_security_policy", []map[string]interface{}{flattenedDefaultSecPolicy})
	}

	if userList != nil {
		d.Set("users", flattenAccessGroupUsers(userList))
	}
}

func flattenNetObjectAccessGroup(noag acsgroups.NetObjectAccessGroup) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"access_group_id": noag.AccessGroupID,
		"id":              noag.ID,
		"version_id":      noag.VersionID,
	}
	res = append(res, temp)
	return res
}

func flattenDefaultSecurityPolicy(dsp acsgroups.DefaultSecurityPolicy) []map[string]interface{} {
	res := make([]map[string]interface{}, 0)
	temp := map[string]interface{}{
		"display_name":              dsp.DisplayName,
		"id":                        dsp.ID,
		"description":               dsp.Description,
		"version_id":                dsp.VersionID,
		"access_group_id":           dsp.AccessGroupID,
		"default_acl_drop":          dsp.DefaultAclDrop,
		"default_open_session_drop": dsp.DefaultOpenSessionDrop,
	}
	res = append(res, temp)
	return res
}

func flattenAccessGroupList(agList *acsgroups.AccessGroupList) []map[string]interface{} {
	if agList == nil {
		return []map[string]interface{}{}
	}
	res := make([]map[string]interface{}, 0, len(agList.AccessGroups))
	for _, v := range agList.AccessGroups {
		temp := map[string]interface{}{
			"id":                      v.ID,
			"display_name":            v.DisplayName,
			"comment":                 v.Comment,
			"created_at":              v.CreatedAt,
			"net_object_access_group": flattenNetObjectAccessGroup(v.NetObjectAccessGroup),
			"default_security_policy": flattenDefaultSecurityPolicy(v.DefaultSecurityPolicy),
		}
		res = append(res, temp)
	}
	return res
}

func flattenAccessGroupUserList(userList *acsgroups.UsersList) []map[string]interface{} {
	if userList == nil {
		return []map[string]interface{}{}
	}
	res := make([]map[string]interface{}, 0, len(userList.Users))
	for _, v := range userList.Users {
		temp := map[string]interface{}{
			"id":           v.ID,
			"display_name": v.Name,
			"role_id":      v.RoleID,
			"login":        v.Login,
		}
		res = append(res, temp)
	}
	return res
}

func flattenAccessGroupUsers(userList *acsgroups.UsersList) []map[string]interface{} {
	if userList == nil {
		return []map[string]interface{}{}
	}
	res := make([]map[string]interface{}, 0, len(userList.Users))
	for _, v := range userList.Users {
		temp := map[string]interface{}{
			"user_id":              v.ID,
			"access_group_role_id": v.RoleID,
		}
		res = append(res, temp)
	}
	return res
}
