/*
 * Copyright (c) 1998-2016 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * version 2.0. If a copy of the MPL was not distributed with this file, You
 * can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as defined
 * by the Mozilla Public License, version 2.0.
 */

package com.trollworks.gcs.character;

import com.trollworks.toolkit.ui.scale.Scale;
import com.trollworks.toolkit.ui.widget.outline.Outline;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.awt.Rectangle;

import javax.swing.JPanel;

/** A panel that holds a pair of side-by-side outlines. */
public class DoubleOutlinePanel extends JPanel implements LayoutManager2 {
	private SingleOutlinePanel	mLeftPanel;
	private SingleOutlinePanel	mRightPanel;

	/**
	 * Creates a new double outline panel.
	 *
	 * @param leftOutline The outline to display on the left.
	 * @param leftTitle The localized title for the left panel.
	 * @param rightOutline The outline to display on the right.
	 * @param rightTitle The localized title for the right panel.
	 * @param useProxy <code>true</code> if a proxy of the outlines should be used.
	 */
	public DoubleOutlinePanel(Scale scale, Outline leftOutline, String leftTitle, Outline rightOutline, String rightTitle, boolean useProxy) {
		super();
		setLayout(this);
		mLeftPanel = new SingleOutlinePanel(scale, leftOutline, leftTitle, useProxy);
		mRightPanel = new SingleOutlinePanel(scale, rightOutline, rightTitle, useProxy);
		add(mLeftPanel);
		add(mRightPanel);
	}

	/**
	 * Sets the embedded outline's display range.
	 *
	 * @param forRight <code>true</code> to set the right outline.
	 * @param first The first row to display.
	 * @param last The last row to display.
	 */
	public void setOutlineRowRange(boolean forRight, int first, int last) {
		(forRight ? mRightPanel : mLeftPanel).setOutlineRowRange(first, last);
	}

	@Override
	public void addLayoutComponent(Component comp, Object constraints) {
		// Not used.
	}

	@Override
	public float getLayoutAlignmentX(Container target) {
		return CENTER_ALIGNMENT;
	}

	@Override
	public float getLayoutAlignmentY(Container target) {
		return CENTER_ALIGNMENT;
	}

	@Override
	public void invalidateLayout(Container target) {
		// Not used.
	}

	@Override
	public void addLayoutComponent(String name, Component comp) {
		// Not used.
	}

	@Override
	public void removeLayoutComponent(Component comp) {
		// Not used.
	}

	@Override
	public Dimension preferredLayoutSize(Container parent) {
		return getLayoutSize(parent, mLeftPanel.getPreferredSize(), mRightPanel.getPreferredSize());
	}

	@Override
	public Dimension minimumLayoutSize(Container parent) {
		return getLayoutSize(parent, mLeftPanel.getMinimumSize(), mRightPanel.getMinimumSize());
	}

	@Override
	public Dimension maximumLayoutSize(Container parent) {
		return getLayoutSize(parent, mLeftPanel.getMaximumSize(), mRightPanel.getMaximumSize());
	}

	@Override
	public void layoutContainer(Container parent) {
		Insets insets = getInsets();
		Rectangle bounds = new Rectangle(insets.left, insets.top, getWidth() - (insets.left + insets.right), getHeight() - (insets.top + insets.bottom));
		Scale scale = Scale.get(parent);
		int one = scale.scale(1);
		int two = one + one;
		int right = bounds.width / 2 - one;
		int left = bounds.width - (two + right);
		mLeftPanel.setBounds(bounds.x, bounds.y, left, bounds.height);
		mRightPanel.setBounds(bounds.x + left + two, bounds.y, right, bounds.height);
	}

	private Dimension getLayoutSize(Container parent, Dimension leftSize, Dimension rightSize) {
		Dimension size = new Dimension(leftSize.width + rightSize.width, Math.max(leftSize.height, rightSize.height));
		Insets insets = getInsets();
		int one = Scale.get(parent).scale(1);
		size.width += insets.left + one + one + insets.right;
		size.height += insets.top + insets.bottom;
		return size;
	}
}

@@ -549,24 +549,47 @@
 e.Outline;%0A%0A
+import java.awt.Color;%0A
 import java.
@@ -1534,16 +1534,46 @@
 (this);%0A
+%09%09setBackground(Color.WHITE);%0A
 %09%09mLeftP
@@ -3449,19 +3449,19 @@
 ;%0A%09%09int 
-one
+gap
  = scale
@@ -3463,25 +3463,25 @@
 scale.scale(
-1
+2
 );%0A%09%09int two
@@ -3481,76 +3481,17 @@
 int 
-two = one + one;%0A%09%09int right = bounds.width / 2 - one;%0A%09%09int left
+width
  = 
+(
 boun
@@ -3505,21 +3505,16 @@
 h - 
-(two + right)
+gap) / 2
 ;%0A%09%09
@@ -3554,20 +3554,21 @@
 unds.y, 
-left
+width
 , bounds
@@ -3616,18 +3616,28 @@
 x + 
-left + two
+bounds.width - width
 , bo
@@ -3644,21 +3644,21 @@
 unds.y, 
-right
+width
 , bounds
@@ -3915,85 +3915,62 @@
 ;%0A%09%09
-int one = Scale.get(parent).scale(1);%0A%09%09size.width += insets.left + one + one
+size.width += insets.left + Scale.get(parent).scale(2)
  + i

Fix gap and background color