/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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.
 */

package org.wso2.carbon.siddhi.editor.core.util.designview.beans.configs;

import java.util.Arrays;

/**
 * Represents Code segment of a Siddhi element in a Siddhi app, which is generated as a config
 */
public class ElementCodeSegment implements Comparable {
    private int[] queryContextStartIndex;
    private int[] queryContextEndIndex;

    public ElementCodeSegment(int[] queryContextStartIndex, int[] queryContextEndIndex) {
        this.queryContextStartIndex = new int[]{queryContextStartIndex[0], queryContextStartIndex[1]};
        this.queryContextEndIndex = new int[]{queryContextEndIndex[0], queryContextEndIndex[1]};
    }

    public int[] getQueryContextStartIndex() {
        return new int[]{queryContextStartIndex[0], queryContextStartIndex[1]};
    }

    public void setQueryContextStartIndex(int[] queryContextStartIndex) {
        this.queryContextStartIndex = new int[]{queryContextStartIndex[0], queryContextStartIndex[1]};
    }

    public int[] getQueryContextEndIndex() {
        return new int[]{queryContextEndIndex[0], queryContextEndIndex[1]};
    }

    public void setQueryContextEndIndex(int[] queryContextEndIndex) {
        this.queryContextEndIndex = new int[]{queryContextEndIndex[0], queryContextEndIndex[1]};
    }

    public int getStartLine() {
        return queryContextStartIndex[0];
    }

    public int getStartColumn() {
        return queryContextStartIndex[1];
    }

    public int getEndLine() {
        return queryContextEndIndex[0];
    }

    public int getEndColumn() {
        return queryContextEndIndex[1];
    }

    public boolean isValid() {
        if (getStartLine() == getEndLine()) {
            return (getEndColumn() - getStartColumn() > 0);
        }
        return (getEndLine() - getStartLine() > 0);
    }

    @Override
    public int compareTo(Object o) {
        ElementCodeSegment otherSegment = (ElementCodeSegment) o;
        if (getStartLine() > otherSegment.getStartLine()) {
            return 1;
        } else if (getStartLine() == otherSegment.getStartLine()) {
            if (getEndLine() == otherSegment.getEndLine()) {
                if (getStartColumn() == otherSegment.getStartColumn()) {
                    return Integer.compare(getEndColumn(), otherSegment.getEndColumn());
                } else {
                    return Integer.compare(getStartColumn(), otherSegment.getStartColumn());
                }
            } else {
                return Integer.compare(getEndLine(), otherSegment.getEndLine());
            }
        } else {
            return -1;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ElementCodeSegment that = (ElementCodeSegment) o;
        return Arrays.equals(getQueryContextStartIndex(), that.getQueryContextStartIndex()) &&
                Arrays.equals(getQueryContextEndIndex(), that.getQueryContextEndIndex());
    }

    @Override
    public int hashCode() {
        int result = Arrays.hashCode(getQueryContextStartIndex());
        result = 31 * result + Arrays.hashCode(getQueryContextEndIndex());
        return result;
    }
}

@@ -925,16 +925,36 @@
 mparable
+%3CElementCodeSegment%3E
  %7B%0A    p
@@ -2503,78 +2503,685 @@
 lic 
-int compareTo(Object o) %7B%0A        ElementCodeSegment otherSegment = (E
+boolean equals(Object o) %7B%0A        if (this == o) return true;%0A        if (o == null %7C%7C getClass() != o.getClass()) %7B%0A            return false;%0A        %7D%0A        ElementCodeSegment that = (ElementCodeSegment) o;%0A        return Arrays.equals(getQueryContextStartIndex(), that.getQueryContextStartIndex()) &&%0A                Arrays.equals(getQueryContextEndIndex(), that.getQueryContextEndIndex());%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        int result = Arrays.hashCode(getQueryContextStartIndex());%0A        result = 31 * result + Arrays.hashCode(getQueryContextEndIndex());%0A        return result;%0A    %7D%0A%0A    @Override%0A    public int compareTo(ElementCodeSegment e
 leme
@@ -3187,34 +3187,33 @@
 entCodeSegment) 
-o;
+%7B
 %0A        if (get
@@ -3226,21 +3226,27 @@
 ine() %3E 
-other
+elementCode
 Segment.
@@ -3314,37 +3314,43 @@
 tStartLine() == 
-other
+elementCode
 Segment.getStart
@@ -3387,29 +3387,35 @@
 ndLine() == 
-other
+elementCode
 Segment.getE
@@ -3466,21 +3466,27 @@
 mn() == 
-other
+elementCode
 Segment.
@@ -3556,37 +3556,43 @@
 getEndColumn(), 
-other
+elementCode
 Segment.getEndCo
@@ -3682,29 +3682,35 @@
 rtColumn(), 
-other
+elementCode
 Segment.getS
@@ -3820,13 +3820,19 @@
 (), 
-other
+elementCode
 Segm
@@ -3923,650 +3923,7 @@
    %7D
-%0A%0A    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) return true;%0A        if (o == null %7C%7C getClass() != o.getClass()) %7B%0A            return false;%0A        %7D%0A        ElementCodeSegment that = (ElementCodeSegment) o;%0A        return Arrays.equals(getQueryContextStartIndex(), that.getQueryContextStartIndex()) &&%0A                Arrays.equals(getQueryContextEndIndex(), that.getQueryContextEndIndex());%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        int result = Arrays.hashCode(getQueryContextStartIndex());%0A        result = 31 * result + Arrays.hashCode(getQueryContextEndIndex());%0A        return result;%0A    %7D
 %0A%7D%0A

Modify implementation of the Comparable interface in ElementCodeSegment