<template>
  <dougal-project-settings-file-matching-parameters
    title="Raw P1/11"
    subtitle="Raw P1/11 files location and parameters."
    v-bind="{rootPath: value.rootPath}"
    v-bind.sync="bind"
    :is-valid="isValid"
    :save="save"
  >
    <template v-slot:paths v-if="validationErrors.includes('ERR_PATHS')">
      <v-alert type="warning">
        At least one path entry is required.<br/>
        <ul>
          <li>If you have final P1/11 files in multiple paths (e.g., each file in its own sequence directory), enter here the parent directory.</li>
          <li>If files are across multiple paths without a common ancestor, you must add multiple entries here.</li>
        </ul>
      </v-alert>
    </template>

    <template v-slot:globs v-if="validationErrors.includes('ERR_GLOBS')">
      <v-alert type="warning">
        At least one glob expression is required.
      </v-alert>
    </template>

    <template v-slot:line-info v-if="validationErrors.includes('ERR_LINEINFO')">
      <v-alert type="warning">
        At least the following fields are required:
        <ul>
          <li><code>line</code> (integer, the preplot line number)</li>
          <li><code>sequence</code> (integer, the acquisition sequence number)</li>
        </ul>
      </v-alert>
    </template>
  </dougal-project-settings-file-matching-parameters>

</template>

<style scoped>
.sample {
  font-family: mono;
  white-space: pre;
}
</style>

<script>
import { deepSet } from '@/lib/utils';
import DougalProjectSettingsFileMatchingParameters from '@/components/project-settings/file-matching-parameters';
import { mapActions, mapGetters } from 'vuex';

export default {
  name: "DougalProjectSettingsRaw111",

  components: {
    DougalProjectSettingsFileMatchingParameters
  },

  props: {
    title: String,
    subtitle: String,
    value: Object
  },

  data () {
    return {
      tab: null,
    }
  },

  computed: {
    // Current working object.
    // A shortcut so we don't have to specify the full path
    // on every input control. It also makes it easier to
    // change that path if necessary. Finally, it ensures that
    // the properties being modified are always available.
    cwo: {

      get () {
        if (this.value) {
          if (!this.value?.raw?.p111) {
            deepSet(this.value, [ "raw", "p111" ], {
              globs: [ "**/*.p111", "**/*.P111" ],
              paths: [],
              lineNameInfo: {
                example: "",
                fields: {
                  line: {
                    length: 4,
                    type: "int"
                  },
                  sequence: {
                    length: 3,
                    type: "int"
                  },
                  incr: {
                    length: 1,
                    type: "bool"
                  },
                  attempt: {
                    length: 1,
                    type: "int"
                  },
                  file_no: {
                    length: 3,
                    type: "int"
                  }
                }
              }
            });
          }
          return this.value.raw.p111;
        } else {
          return {
              globs: [ "**/*.p111", "**/*.P111" ],
              paths: [],
              lineNameInfo: {
                example: "",
                fields: {
                  line: {
                    length: 4,
                    type: "int"
                  },
                  sequence: {
                    length: 3,
                    type: "int"
                  },
                }
              }
            };
        }
      },

      set (v) {
        if (this.value) {
          deepSet(this.value, [ "raw", "p111" ], v);
        }
      }
    },

    bind () {
      return {
        globs: this.cwo?.globs,
        paths: this.cwo?.paths,
        pattern: this.cwo?.pattern,
        lineNameInfo: this.cwo?.lineNameInfo
      };
    },

    validationErrors () {
      const errors = [];

      if (!this.cwo?.paths.length || !this.cwo?.paths[0].length) {
        // "Missing path: we need at least one directory where to search for matching files"
        errors.push("ERR_PATHS");
      }

      if (!this.cwo?.globs.length) {
        // "Missing globs: we need at least one glob to search for matching files"
        errors.push("ERR_GLOBS");
      }

      if (this.cwo?.lineNameInfo) {
        const pass = !this.cwo?.lineNameInfo?.fields || [ "line", "sequence" ].every( i =>
          ["offset", "length"].every( j => j in (this.cwo?.lineNameInfo?.fields?.[i] ?? {}) ));

        if (!pass) {
          // "Missing field info: We need at least 'line' and 'sequence' fields"
          errors.push("ERR_LINEINFO")
        }
      }

      return errors;
    },

    isValid () {
      return this.validationErrors.length == 0;
    }

  },

  watch: {
  },

  methods: {

    reset () {
    },

    save () {
    },

    back () {
      this.$emit('close');
    },
  },

  mounted () {
    this.reset();
  }

}
</script>
