classdef surfaceIslandPolygon < handle
% surfaceIslandPolygon creates a surfaceIslandPolygon object that returns
% the surface island polygon.
%
% S = surfaceIslandPolygon(sobj,SurfInd) creates a island polygon of 
% metalSurface w.r.t the surface index.
%
% surfaceIslandPolygon input argument:
%   sobj     - metalSurface object.
%   SurfInd  - Index of the surface in a metalSurface. It accepts the
%              scalar value.
%
% surfaceIslandPolygon properties:
%   NumPolygons - Number of island polygon in a surface
%   Vertices    - Vertices of the island polygon.
%
% surfaceIslandPolygon methods:   
%   shapes  - Returns the shape of the island polygon in the surface

% Copyright 2023 The MathWorks, Inc.

    properties(SetAccess = private, GetAccess = public)
        % Number of island polygon
        NumPolygons
        % Vertice of island polygon
        Vertices
        % Shape of the island polygon
        %Shape
    end

    properties(Access=private)
        PrivateSurfaceInd
        PrivateLayerNumber
        PrivateCadnetName
        PrivateInputType
    end

    methods
        function obj = surfaceIslandPolygon(Sobj,SurfInd)
            arguments
                Sobj(1,1)  metalSurface
                SurfInd(1,1) {mustBeNumeric,...
                    mustBeNonNan,mustBeFinite,mustBeInteger,...
                    mustBePositive,mustBeValidNumber(Sobj,SurfInd)};
            end
            obj.PrivateInputType = Sobj.InputType;
            if strcmpi( obj.PrivateInputType,'Layer')
                obj.PrivateLayerNumber = Sobj.LayerNumber;
            elseif strcmpi( obj.PrivateInputType,'Cadnet')
                obj.PrivateCadnetName = Sobj.CadnetName;
            end
            obj.PrivateSurfaceInd = SurfInd;
        end
    end
    methods %-------Set/Get-------- %
        function PolyCnt = get.NumPolygons(obj)
            PolyCnt = 1;
            obj.NumPolygons =  PolyCnt;
        end

        function IslandVertices = get.Vertices(obj)
            if ~isempty(obj.PrivateLayerNumber)
                layerID = int32(obj.PrivateLayerNumber);
                Surf_LayerId = getLayerSurfaceId(layerID);
                Surf_LayerId = Surf_LayerId';
                surfID = Surf_LayerId(obj.PrivateSurfaceInd);
            else
                Surf_LayerId = getCadnetSurfaceIndexList(obj.PrivateCadnetName)';
                surfID = Surf_LayerId(obj.PrivateSurfaceInd);
            end
            [IxValues, IyValues] = getSurfaceIslandPolygonData(int32(surfID));
            IslandVertices = [IxValues(:), IyValues(:)];
            obj.Vertices =  IslandVertices;
        end

        % function IslandPoly = get.Shape(obj)
        %     IslandPoly = CreateIslandPolygon(obj,obj.PrivateSurfaceInd);
        %     obj.Shape =  IslandPoly;
        % end
    end

    methods (Hidden)
        function   SurfIslandPoly = CreateIslandPolygon(obj,surfInd)
            orig_state = warning;
            warning('off','MATLAB:polyshape:boundary3Points');
            if ~isempty(obj.PrivateLayerNumber)
                layerID = int32(obj.PrivateLayerNumber);
                Surf_LayerId = getLayerSurfaceId(layerID);
                Surf_LayerId = Surf_LayerId';
                surfID = Surf_LayerId(surfInd);
            else
                Surf_LayerId = getCadnetSurfaceIndexList(obj.PrivateCadnetName)';
                surfID = Surf_LayerId(surfInd);
            end

            IpolygonVertices = [nan,nan];
            Iseparator = [nan,nan];
            %Draw the Island
            [IxValues, IyValues] = getSurfaceIslandPolygonData(int32(surfID));
            IslandVertices = [IxValues(:), IyValues(:)];
            IpolygonVertices = [IpolygonVertices; Iseparator; IslandVertices];
            SurfIslandPoly = antenna.Polygon('Vertices',IpolygonVertices);
            warning(orig_state);
            
        end
    end

    methods
        function S = shapes(obj)
            %shapes returns the shape of the island polygon in a surfaces. 
            %
            % S = shapes(obj) returns the shapes of island polygon in a 
            % surfaces. 
            %

            % Copyright 2023 The MathWorks, Inc.

            surfInd = obj.PrivateSurfaceInd;
            SurfIslandPoly = CreateIslandPolygon(obj,surfInd);

            if ~isempty(SurfIslandPoly)
                S = SurfIslandPoly;
            % else
            %     S = [];
            end
        end
    end
end
function mustBeValidNumber(SurfObj,value)
SurfaceInd = value;
SurfaceIndExp = SurfObj.SurfaceInfo.SurfaceIndex;
if any(SurfaceInd==SurfaceIndExp)
    value = value;  %#ok<ASGSL>
else
    eid = 'Value:notValid';
    msg = getString(message('rfpcb:rfpcberrors:SurfaceIndex',num2str(SurfaceIndExp')));
    throwAsCaller(MException(eid,msg));
end
end