/*
 * Copyright 2018-present Facebook, 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.
 */

package com.facebook.buck.intellij.ideabuck.config;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

/** Persistent per-cell preferences in a {@link com.intellij.openapi.project.Project}. */
public class BuckCell {
  public String name = "";
  public String root = "$PROJECT_DIR$";
  public String buildFileName = "BUCK";

  /** Returns a copy of this cell. */
  public BuckCell copy() {
    BuckCell result = new BuckCell();
    result.name = this.name;
    result.root = this.root;
    result.buildFileName = this.buildFileName;
    return result;
  }

  /**
   * Returns the name of this cells (as in the first component of targets formatted as {@code
   * cellname//path/to:target}).
   */
  public String getName() {
    return name;
  }

  /** Sets the name of this cell. */
  public void setName(String name) {
    this.name = Preconditions.checkNotNull(name);
  }

  /**
   * Returns the root directory of this cell.
   *
   * <p>Note that the path may contain unexpanded {@link
   * com.intellij.openapi.application.PathMacros}, such as {@code $PROJECT_DIR$} or {@code
   * $USER_HOME$}.
   */
  public String getRoot() {
    return root;
  }

  /** Sets the root directory of this cell. */
  public void setRoot(String root) {
    this.root = Preconditions.checkNotNull(root);
  }

  /**
   * The name of Buck files for this cell.
   *
   * @see {@url https://buckbuild.com/concept/buckconfig.html#buildfile.name The buck config setting
   *     for <code>buildfile.name</code>}
   */
  public String getBuildFileName() {
    return buildFileName;
  }

  /** Sets the name of Buck files for this cell. */
  public void setBuildFileName(String buildFileName) {
    this.buildFileName = Preconditions.checkNotNull(buildFileName);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    BuckCell buckCell = (BuckCell) o;
    return Objects.equal(name, buckCell.name)
        && Objects.equal(root, buckCell.root)
        && Objects.equal(buildFileName, buckCell.buildFileName);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(name, root, buildFileName);
  }
}

@@ -653,16 +653,72 @@
 onfig;%0A%0A
+import com.google.common.annotations.VisibleForTesting;%0A
 import c
@@ -748,16 +748,16 @@
 bjects;%0A
-
 import c
@@ -1558,32 +1558,219 @@
 ull(name);%0A  %7D%0A%0A
+  /** Builder pattern for creating buck cells. */%0A  @VisibleForTesting%0A  public BuckCell withName(String name) %7B%0A    BuckCell copy = copy();%0A    copy.setName(name);%0A    return copy;%0A  %7D%0A%0A
   /**%0A   * Retur
@@ -2168,24 +2168,211 @@
 root);%0A  %7D%0A%0A
+  /** Builder pattern for creating buck cells. */%0A  @VisibleForTesting%0A  public BuckCell withRoot(String root) %7B%0A    BuckCell copy = copy();%0A    copy.setRoot(root);%0A    return copy;%0A  %7D%0A%0A
   /**%0A   * T
@@ -2802,32 +2802,255 @@
 FileName);%0A  %7D%0A%0A
+  /** Builder pattern for creating buck cells. */%0A  @VisibleForTesting%0A  public BuckCell withBuildFileName(String buildFileName) %7B%0A    BuckCell copy = copy();%0A    copy.setBuildFileName(buildFileName);%0A    return copy;%0A  %7D%0A%0A
   @Override%0A  pu
@@ -3438,24 +3438,24 @@
 ashCode() %7B%0A
-
     return O
@@ -3501,11 +3501,264 @@
 me);%0A  %7D
+%0A%0A  @Override%0A  public String toString() %7B%0A    return %22BuckCell%7B%22%0A        + %22name='%22%0A        + name%0A        + '%5C''%0A        + %22, root='%22%0A        + root%0A        + '%5C''%0A        + %22, buildFileName='%22%0A        + buildFileName%0A        + '%5C''%0A        + '%7D';%0A  %7D
 %0A%7D%0A

Add builder-like methods to BuckCell.