import {Panel} from '../../helix/javascript/array_summary/panel';
import Colors from '../../helix/javascript/array_summary/colors';

describe("A Panel", function () {
  var panelLocation = {x: 10, y: 20, width: 2, height: 1.5};
  var subject;
  var shapeSpy;
  var panelData;
  var graphicsSpy;

  beforeEach(function () {
    var graphicsMethods = ["setStrokeStyle", "beginStroke", "beginFill", "drawRect",
      "endStroke", "endFill", "closePath", "moveTo", "lineTo"];
    graphicsSpy = jasmine.createSpyObj("graphics", graphicsMethods);
    graphicsMethods.forEach(function (method) {
      graphicsSpy[method].and.returnValue(graphicsSpy);
    });
    shapeSpy = {
      dispatchEvent: jasmine.createSpy("dispatchEvent"),
      graphics: graphicsSpy
    };
    spyOn(createjs, "Shape").and.returnValue(shapeSpy);
    panelData = {
      x: 1,
      y: 2,
      width: 2,
      height: 1.5,
      data: {
        ballast: 20,
        wind_anchors: 2,
        seismic_anchors: 1,
        wind_zones: 'C',
        cross_trays: 1,
        link_trays: 1,
        panel_id: 1,
        subarray: 5,
        panel_type: 4,
        psf: 3.2379
      }
    };
    subject = new Panel(panelData);
  });

  it("locates itself at the panel coordinates", function () {
    expect(subject.x).toEqual(panelLocation.x - subject.width / 2);
    expect(subject.y).toEqual(panelLocation.y - subject.height / 2);
  });

  it("has a bounds based on the width and height given", function () {
    expect(graphicsSpy.drawRect).toHaveBeenCalledWith(0, 0, 20, 15);
  });

  it("has a single shape child", function () {
    expect(subject.children).toContain(shapeSpy);
  });

  describe("background color with anchors", function () {
    it("shows a panel with no anchors in navy", function () {
      panelData.data.seismic_anchors = 0;
      panelData.data.wind_anchors = 0;

      subject = new Panel(panelData);
      expect(graphicsSpy.beginFill).toHaveBeenCalledWith(Colors.default_background);
    });

    it("shows the seismic anchors in yellow", function () {
      panelData.data.seismic_anchors = 2;
      panelData.data.wind_anchors = 0;

      subject = new Panel(panelData);
      expect(graphicsSpy.beginFill).toHaveBeenCalledWith(Colors.seismic_background);
    });

    it("shows the wind anchors in light-blue", function () {
      panelData.data.seismic_anchors = 0;
      panelData.data.wind_anchors = 1;

      subject = new Panel(panelData);
      expect(graphicsSpy.beginFill).toHaveBeenCalledWith(Colors.wind_background);
    });

    it("shows a panel with both wind and seismic anchors with a split color", function () {
      panelData.data.seismic_anchors = 1;
      panelData.data.wind_anchors = 2;

      subject = new Panel(panelData);
      expect(graphicsSpy.beginFill).toHaveBeenCalledWith(Colors.wind_background);
      expect(graphicsSpy.beginFill).toHaveBeenCalledWith(Colors.seismic_background);
    });
  });

  describe("selecting the panel", function () {
    beforeEach(function () {
      subject.select();
    });

    it("marks the panel as selected", function () {
      expect(subject.selected).toBeTruthy();
    });

    it("changes the background color", function () {
      expect(graphicsSpy.beginFill).toHaveBeenCalledWith('white');
    });
  });

  describe("changing the overlay", function () {
    describe("to ANCHOR", function () {
      beforeEach(function () {
        subject.setOverlay("ANCHOR");
      });

      it("shows the ballast information", function () {
        expect(subject.children).toContain(subject.textOverlays.ballast);
        expect(subject.textOverlays.ballast.text).toEqual(20);
      });

      it("shows the sum of the anchor counts", function () {
        expect(subject.children).toContain(subject.textOverlays.anchors);
        expect(subject.textOverlays.anchors.text).toEqual(3);
      });

      it("does not show the other information", function () {
        expect(subject.textOverlays.hasOwnProperty('panel_id')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('wind_anchors')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('seismic_anchors')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('wind_zones')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('cross_trays')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('link_trays')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('subarray')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('psf')).toBeFalsy();
        expect(subject.textOverlays.hasOwnProperty('panel_type')).toBeFalsy();
      });
    });

    describe("to ALL", function () {
      beforeEach(function () {
        subject.setOverlay("ALL");
      });

      it("shows the panel_id information", function () {
        expect(subject.textOverlays.panel_id.text).toEqual(1);
        expect(subject.children).toContain(subject.textOverlays.panel_id);
      });

      it("shows the ballast information", function () {
        expect(subject.children).toContain(subject.textOverlays.ballast);
        expect(subject.textOverlays.ballast.text).toEqual(20);
      });

      it("shows the anchor information", function () {
        expect(subject.children).toContain(subject.textOverlays.wind_anchors);
        expect(subject.children).toContain(subject.textOverlays.seismic_anchors);
        expect(subject.textOverlays.wind_anchors.text).toEqual(2);
        expect(subject.textOverlays.seismic_anchors.text).toEqual('S');
      });

      it("shows the wind zone information", function () {
        expect(subject.textOverlays.wind_zones.text).toEqual('C');
        expect(subject.children).toContain(subject.textOverlays.wind_zones);
      });

      it("shows the cross trays information", function () {
        expect(subject.textOverlays.cross_trays.text).toEqual(1);
        expect(subject.children).toContain(subject.textOverlays.cross_trays);
      });

      it("shows the link trays information", function () {
        expect(subject.textOverlays.link_trays.text).toEqual(1);
        expect(subject.children).toContain(subject.textOverlays.link_trays);
      });

      it("shows the psf information", function () {
        expect(subject.textOverlays.psf.text).toEqual('3.24');
        expect(subject.children).toContain(subject.textOverlays.link_trays);
      });

      it("shows the subarray information", function () {
        expect(subject.textOverlays.subarray.text).toEqual(5);
        expect(subject.children).toContain(subject.textOverlays.link_trays);
      });

      it("shows the panel type information", function () {
        expect(subject.textOverlays.panel_type.text).toEqual(4);
        expect(subject.children).toContain(subject.textOverlays.panel_type);
      });
    });
  });
});
