/*
 * Copyright (c) 2007-2010 by The Broad Institute, Inc. and the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL), Version 2.1 which
 * is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 *
 * THE SOFTWARE IS PROVIDED "AS IS." THE BROAD AND MIT MAKE NO REPRESENTATIONS OR WARRANTIES OF
 * ANY KIND CONCERNING THE SOFTWARE, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT
 * OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE.  IN NO EVENT SHALL THE BROAD OR MIT, OR THEIR
 * RESPECTIVE TRUSTEES, DIRECTORS, OFFICERS, EMPLOYEES, AND AFFILIATES BE LIABLE FOR ANY DAMAGES OF
 * ANY KIND, INCLUDING, WITHOUT LIMITATION, INCIDENTAL OR CONSEQUENTIAL DAMAGES, ECONOMIC
 * DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER THE BROAD OR MIT SHALL
 * BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE
 * FOREGOING.
 */

/*
 * CytobandRenderer.java
 *
 * Created on September 19, 2007, 5:36 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.broad.igv.renderer;

import org.broad.igv.feature.Cytoband;
import org.broad.igv.session.ViewContext;
import org.broad.igv.ui.FontManager;
import org.broad.igv.ui.IGVMainFrame;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jrobinso
 */
public class CytobandRenderer {

    boolean drawLabels = true;
    static final public int CYTOBAND_Y_OFFSET = 5;
    static final public int LABEL_OFFSET = 25;
    private static Map<Integer, Color> stainColors =
            new HashMap<Integer, Color>();

    public void draw(List<Cytoband> data, Graphics g2D, Rectangle graphicRect) {

        if (data.size() > 0) {

            // If we are in the process of exporting to an image file
            // we need to write out the cytoband locus on the image
            if (IGVMainFrame.getInstance().isExportingSnapshot()) {
                String locus = IGVMainFrame.getInstance().getDisplayedLocusString();
                if (locus != null) {
                    Graphics g2 = g2D.create();
                    g2.setFont(FontManager.getScalableFont(Font.BOLD, 10));
                    g2.drawString(locus, 3, 11);
                }
            }

            // Draw Cytoband
            drawBands(data, g2D, graphicRect);

            // Draw Cytoband Labels
            if (drawLabels) {
                drawLabels(g2D, graphicRect);
            }
        }
    }

    public void drawBands(List<Cytoband> data, Graphics g2D, Rectangle graphicRect) {

        int[] xC = new int[3];
        int[] yC = new int[3];

        double scale =
                ((double) graphicRect.getWidth()) / getViewContext().getChromosomeLength();

        for (Cytoband cytoband : data) {

            int start = (int) (graphicRect.getX() + scale * cytoband.getStart());
            int end = (int) (graphicRect.getX() + scale * cytoband.getEnd());
            int y = (int) graphicRect.getY() + CYTOBAND_Y_OFFSET;
            int height = (int) graphicRect.getHeight();

            if (cytoband.getType() == 'c') { // centermere: "acen"

                int center = (int) (y + height / 2);
                if (cytoband.getName().startsWith("p")) {
                    xC[0] = (int) start;
                    yC[0] = (int) graphicRect.getMaxY() + CYTOBAND_Y_OFFSET;
                    xC[1] = (int) start;
                    yC[1] = y;
                    xC[2] = (int) end;
                    yC[2] = center;
                } else {
                    xC[0] = (int) end;
                    yC[0] = (int) graphicRect.getMaxY() + CYTOBAND_Y_OFFSET;
                    xC[1] = (int) end;
                    yC[1] = y;
                    xC[2] = (int) start;
                    yC[2] = center;
                }
                g2D.setColor(Color.RED.darker());
                g2D.fillPolygon(xC, yC, 3);
            } else {

                g2D.setColor(getCytobandColor(cytoband));
                g2D.fillRect(start, y, (end - start), height);
                g2D.setColor(Color.BLACK);
                g2D.drawRect(start, y, (end - start), height);

            }
        }
    }

    private void drawLabels(final Graphics g, Rectangle graphicRect) {

        double width = graphicRect.getWidth();
        int y = (int) graphicRect.getY() + LABEL_OFFSET;

        // Draw labels
        g.setColor(Color.BLACK);
        FontMetrics fm = g.getFontMetrics();
        int minSpacing = 10;
        int prevEnd = 0;
        double sc = width / getViewContext().getChromosomeLength();
        int adjustedY = y;
        for (Cytoband cytoband : getViewContext().getChromosome().getCytobands()) {
            int s = (int) (sc * cytoband.getStart());
            int e = (int) (sc * cytoband.getEnd());
            int stringWidth = (int) fm.getStringBounds(cytoband.getName(), g).getWidth();
            int x = (int) (s + (e - s - stringWidth) / 2);
            if (x > (prevEnd + minSpacing)) {
                g.drawString(cytoband.getName(), x, adjustedY);
                prevEnd = x + stringWidth;
            }
        }
    }

    //Refactor -- remove reference to IGVMainFrame
    private ViewContext getViewContext() {
        return ViewContext.getInstance();
    }

    public void applyTextTranslationAndRotation(Graphics2D graphics2, int x, int y) {

        AffineTransform transform = new AffineTransform();
        transform.translate(x, y);
        transform.rotate(-Math.PI / 2);
        graphics2.transform(transform);
    }

    private static Color getCytobandColor(Cytoband data) {
        if (data.getType() == 'p') { // positive: "gpos100"
            int stain = data.getStain(); // + 4;

            int shade = (int) (255 - stain / 100.0 * 255);
            Color c = stainColors.get(shade);
            if (c == null) {
                c = new Color(shade, shade, shade);
                stainColors.put(shade, c);
            }

            return c;

        } else if (data.getType() == 'c') { // centermere: "acen"
            return Color.PINK;

        } else {
            return Color.WHITE;
        }
    }
}
