<commit_before>/*
 * Copyright 2019 ThoughtWorks, Inc.
 *
 * 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.
 */

// utils
import {override} from "helpers/css_proxies";
import {sha256} from "helpers/digest";
import _ from "lodash";
import m from "mithril";
import Stream from "mithril/stream";

// components
import {MithrilComponent} from "jsx/mithril-component";
import {SelectField, SelectFieldOptions} from "views/components/forms/input_fields";
import {AdvancedSettings} from "views/pages/pipelines/advanced_settings";
import {EnvironmentVariablesEditor} from "views/pages/pipelines/environment_variables_editor";
import {JobEditor} from "views/pages/pipelines/job_editor";
import {MaterialEditor} from "views/pages/pipelines/material_editor";
import {PipelineConfigVMAware} from "views/pages/pipelines/pipeline_config_view_model";
import {PipelineInfoEditor} from "views/pages/pipelines/pipeline_info_editor";
import {StageEditor} from "views/pages/pipelines/stage_editor";
import {TaskTerminalField} from "views/pages/pipelines/task_editor";
import {UserInputPane} from "views/pages/pipelines/user_input_pane";

// CSS
import * as defaultStyles from "views/pages/pac/styles.scss";
import * as uipStyles from "views/pages/pipelines/user_input_pane.scss";

const uipCss: typeof uipStyles = override(uipStyles, {
  userInput: [uipStyles.userInput, defaultStyles.logicalSection].join(" ")
});

interface Attrs extends PipelineConfigVMAware {
  onContentChange?: (changed: boolean) => void;
}

export class BuilderForm extends MithrilComponent<Attrs> {
  private hash = Stream("");

  oncreate(vnode: m.VnodeDOM<Attrs, {}>) {
    vnode.dom.addEventListener("change", (e) => {
      const vm = vnode.attrs.vm;
      sha256(vm.pluginId() + JSON.stringify(vm.pipeline.toApiPayload())).
        then((hash) => {
          if ("function" === typeof vnode.attrs.onContentChange) {
            vnode.attrs.onContentChange(hash !== this.hash());
            this.hash(hash);
          }
        }).catch((err) => console.warn(err)); // tslint:disable-line no-console
    });
  }

  view(vnode: m.Vnode<Attrs>) {
    const { pipeline, material, isUsingTemplate, pluginId } = vnode.attrs.vm;

    return <div class={defaultStyles.builderForm}>
      <UserInputPane css={uipCss} heading="Part 0: Select Config Language">
        <SelectField property={pluginId} label="Choose a Pipelines as Code plugin">
          <SelectFieldOptions selected={pluginId()} items={vnode.attrs.vm.exportPlugins()}/>
        </SelectField>
      </UserInputPane>

      <UserInputPane css={uipCss} heading="Part 1: Material">
        <MaterialEditor material={material}/>
      </UserInputPane>

      <UserInputPane css={uipCss} heading="Part 2: Pipeline Name">
        <PipelineInfoEditor pipelineConfig={pipeline} isUsingTemplate={isUsingTemplate}/>
      </UserInputPane>

      <PipelineBodyEditor {...vnode.attrs}/>
    </div>;
  }
}

class PipelineBodyEditor extends MithrilComponent<Attrs> {
  view(vnode: m.Vnode<Attrs>) {
    const { stage, job } = vnode.attrs.vm;

    return vnode.attrs.vm.whenTemplateAbsent(() => [
      <UserInputPane css={uipCss} heading="Part 3: Stage Details">
        <StageEditor stage={stage} />
      </UserInputPane>,

      <UserInputPane css={uipCss} heading="Part 4: Job and Tasks"><commit_msg>Hide the PaC plugin dropdown on the PaC test drive form for now until we make some UX decisions<commit_after>/*
 * Copyright 2019 ThoughtWorks, Inc.
 *
 * 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.
 */

// utils
import {override} from "helpers/css_proxies";
import {sha256} from "helpers/digest";
import _ from "lodash";
import m from "mithril";
import Stream from "mithril/stream";

// components
import {MithrilComponent} from "jsx/mithril-component";
import {SelectField, SelectFieldOptions} from "views/components/forms/input_fields";
import {AdvancedSettings} from "views/pages/pipelines/advanced_settings";
import {EnvironmentVariablesEditor} from "views/pages/pipelines/environment_variables_editor";
import {JobEditor} from "views/pages/pipelines/job_editor";
import {MaterialEditor} from "views/pages/pipelines/material_editor";
import {PipelineConfigVMAware} from "views/pages/pipelines/pipeline_config_view_model";
import {PipelineInfoEditor} from "views/pages/pipelines/pipeline_info_editor";
import {StageEditor} from "views/pages/pipelines/stage_editor";
import {TaskTerminalField} from "views/pages/pipelines/task_editor";
import {UserInputPane} from "views/pages/pipelines/user_input_pane";

// CSS
import * as defaultStyles from "views/pages/pac/styles.scss";
import * as uipStyles from "views/pages/pipelines/user_input_pane.scss";

const uipCss: typeof uipStyles = override(uipStyles, {
  userInput: [uipStyles.userInput, defaultStyles.logicalSection].join(" ")
});

interface Attrs extends PipelineConfigVMAware {
  onContentChange?: (changed: boolean) => void;
}

export class BuilderForm extends MithrilComponent<Attrs> {
  private hash = Stream("");

  oncreate(vnode: m.VnodeDOM<Attrs, {}>) {
    vnode.dom.addEventListener("change", (e) => {
      const vm = vnode.attrs.vm;
      sha256(vm.pluginId() + JSON.stringify(vm.pipeline.toApiPayload())).
        then((hash) => {
          if ("function" === typeof vnode.attrs.onContentChange) {
            vnode.attrs.onContentChange(hash !== this.hash());
            this.hash(hash);
          }
        }).catch((err) => console.warn(err)); // tslint:disable-line no-console
    });
  }

  view(vnode: m.Vnode<Attrs>) {
    const { pipeline, material, isUsingTemplate, pluginId } = vnode.attrs.vm;

    return <div class={defaultStyles.builderForm}>
      <div style="display: none;">
        <UserInputPane css={uipCss} heading="Part 0: Select Config Language">
          <SelectField property={pluginId} label="Choose a Pipelines as Code plugin">
            <SelectFieldOptions selected={pluginId()} items={vnode.attrs.vm.exportPlugins()}/>
          </SelectField>
        </UserInputPane>
      </div>

      <UserInputPane css={uipCss} heading="Part 1: Material">
        <MaterialEditor material={material}/>
      </UserInputPane>

      <UserInputPane css={uipCss} heading="Part 2: Pipeline Name">
        <PipelineInfoEditor pipelineConfig={pipeline} isUsingTemplate={isUsingTemplate}/>
      </UserInputPane>

      <PipelineBodyEditor {...vnode.attrs}/>
    </div>;
  }
}

class PipelineBodyEditor extends MithrilComponent<Attrs> {
  view(vnode: m.Vnode<Attrs>) {
    const { stage, job } = vnode.attrs.vm;

    return vnode.attrs.vm.whenTemplateAbsent(() => [
      <UserInputPane css={uipCss} heading="Part 3: Stage Details">
        <StageEditor stage={stage} />
      </UserInputPane>,

      <UserInputPane css={uipCss} heading="Part 4: Job and Tasks">
{'fraction_non_alphanumeric': 0.12438835951583826, 'fraction_numerical': 0.004893123873293845, 'mean_word_length': 4.2338274932614555, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7392462', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2301, 'n_words': 591}