//
// Copyright (C) 2016 Jonathon Love
//

syntax = "proto3";

package jamovi.coms;

// ANALYSIS

enum Status {
    COMPLETE = 0;
    IN_PROGRESS = 1;
    ERROR = 2;
}

message Error {
    string message = 1;
    string cause = 2;
}

message ComsMessage {
    int32 id = 1;
    string instanceId = 2;
    bytes payload = 3;
    string payloadType = 4;
    Status status = 5;
    Error error = 6;
}

message AnalysisRequest {
    string datasetId = 1;
    int32 analysisId = 2;
    string name = 3;
    string ns = 4;

    enum Perform {
        INIT = 0;
        RUN = 1;
        RENDER = 4;
        SAVE = 5;
        DELETE = 6;
    }

    Perform perform = 5;
    AnalysisOptions options = 6;
    // int32 ppi = 7;
    repeated string changed = 8;
    int32 revision = 9;
    bool restartEngines = 10;
    bool clearState = 11;

    string path = 16;
    string part = 17;
    string format = 18;
}

enum AnalysisStatus {
    ANALYSIS_NONE = 0;
    ANALYSIS_INITED = 1;
    ANALYSIS_RUNNING = 2;
    ANALYSIS_COMPLETE = 3;
    ANALYSIS_ERROR = 4;
    ANALYSIS_RENDERING = 5;
}

message AnalysisResponse {

    string datasetId = 1;
    int32 analysisId = 2;
    string name = 3;
    string ns = 4;
    // Version version = 5;
    AnalysisOptions options = 6;
    ResultsElement results = 7;
    AnalysisStatus status = 8;
    Error error = 9;
    bool incAsText = 10;
    int32 revision = 11;
    bool restartEngines = 12;
    string stacktrace = 13;
    uint32 version = 14;
}

message AnalysisOption {

    enum Other {
        FALSE = 0;
        TRUE  = 1;
        NULL  = 2;
    }

    oneof type {
        int32 i = 1;
        double d = 2;
        string s = 3;
        Other o = 4;
        AnalysisOptions c = 5;
    }
}

message AnalysisOptions {
    repeated AnalysisOption options = 1;
    bool hasNames = 2;
    repeated string names = 3;
}

enum Visible {
    DEFAULT_YES = 0;
    DEFAULT_NO = 1;
    YES = 2;
    NO = 3;
}

message ResultsCell {

    enum Other {
        MISSING = 0;
        NOT_A_NUMBER = 1;
    }

    oneof cellType {
        int32  i = 1;
        double d = 2;
        string s = 3;
        Other  o = 4;
    }

    int32 format = 5; // bit field

    // 1 = begin group
    // 2 = end group
    // 4 = negative (red highlight)

    repeated string footnotes = 6;
    repeated string symbols = 7;
    int32 sortKey = 8;
}

message ResultsColumn {

    string name = 1;
    string title = 2;
    string type = 3;
    string format = 4;
    string superTitle = 5;
    bool   combineBelow = 6;

    repeated ResultsCell cells = 7;

    bool sortable = 13;
    bool hasSortKeys = 14;
    Visible visible = 15;
}

message ResultsTableNote {
    string key  = 1;
    string note = 2;
    bool   init = 3;
}

message Sort {
    string sortBy = 1;
    bool   sortDesc = 2;
}

message ResultsTable {
    repeated ResultsColumn columns = 1;
    repeated string rowNames = 2;
    bool swapRowsColumns = 3;
    repeated ResultsTableNote notes = 4;
    string asText = 5;
    string rowSelect = 6;
    int32 rowSelected = 7;
    string sortSelect = 8;
    Sort sortSelected = 9;
}

message ResultsImage {
    string path = 1;
    int32 width = 2;
    int32 height = 3;
}

message ResultsArray {
    repeated ResultsElement elements = 1;

    enum LayoutType {
        FLAT = 0;
        LIST_SELECT = 1;
    }

    LayoutType layout = 2;
    bool hideHeadingOnlyChild = 4;
}

message ResultsGroup {
    repeated ResultsElement elements = 1;
}

message ResultsHtml {
    string content = 1;
    repeated string scripts = 2;
    repeated string stylesheets = 3;
}

message ResultsElement {
    string name = 1;
    string title = 2;
    AnalysisStatus status = 3;
    Error error = 4;
    bool stale = 5;

    oneof type {
        ResultsTable table = 6;
        ResultsImage image = 7;
        ResultsGroup group = 8;
        ResultsArray array = 9;
        string preformatted = 10;
        string syntax = 11;
        ResultsHtml html = 12;
    }

    bytes state = 14;
    Visible visible = 15;
}
