package name.abuchen.portfolio.ui.wizards.security;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import name.abuchen.portfolio.ui.Images;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.ui.util.BindingHelper;
import name.abuchen.portfolio.ui.util.DesktopAPI;
import name.abuchen.portfolio.ui.util.SWTHelper;

public class SecurityMasterDataPage extends AbstractPage
{
    private final EditSecurityModel model;
    private final BindingHelper bindings;

    private Text isin;
    private Text wkn;

    protected SecurityMasterDataPage(EditSecurityModel model, BindingHelper bindings)
    {
        this.model = model;
        this.bindings = bindings;

        setTitle(Messages.EditWizardMasterDataTitle);
    }

    @Override
    public void createControl(Composite parent)
    {
        Composite container = new Composite(parent, SWT.NULL);
        setControl(container);
        GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(container);

        boolean isExchangeRate = model.getSecurity().isExchangeRate();
        boolean isSyncedOnline = model.getOnlineId() != null;

        if (isSyncedOnline)
        {
            // empty cell
            new Label(container, SWT.NONE).setText(""); //$NON-NLS-1$

            Composite area = new Composite(container, SWT.NONE);
            RowLayout layout = new RowLayout();
            layout.center = true;
            area.setLayout(layout);

            Link link = new Link(area, SWT.UNDERLINE_LINK);
            link.setText(Messages.LabelLinkedToPortfolioReport);
            link.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> DesktopAPI
                            .browse("https://www.portfolio-report.net/securities/" + model.getOnlineId()))); //$NON-NLS-1$

            Button unlink = new Button(area, SWT.PUSH);
            unlink.setText(Messages.EditWizardMasterDataUnlink);
            unlink.setToolTipText(Messages.EditWizardMasterDataUnlink_ToolTip);
            unlink.setImage(Images.ONLINE.image());
            unlink.addSelectionListener(SelectionListener.widgetSelectedAdapter(event -> {
                model.setOnlineId(null);
                isin.setEnabled(true);
                wkn.setEnabled(true);
                link.setEnabled(false);
                unlink.setEnabled(false);
            }));
        }

        ComboViewer currencyCode = bindings.bindCurrencyCodeCombo(container, Messages.ColumnCurrency, "currencyCode", //$NON-NLS-1$
                        !isExchangeRate);
        if (model.getSecurity().hasTransactions(model.getClient()))
        {
            currencyCode.getCombo().setEnabled(false);

            // empty cell
            new Label(container, SWT.NONE).setText(""); //$NON-NLS-1$

            Composite info = new Composite(container, SWT.NONE);
            info.setLayout(new RowLayout());

            Label l = new Label(info, SWT.NONE);
            l.setImage(Images.INFO.image());

            l = new Label(info, SWT.NONE);
            l.setText(Messages.MsgInfoChangingCurrencyNotPossible);

        }

        if (isExchangeRate)
        {
            ComboViewer targetCurrencyCode = bindings.bindCurrencyCodeCombo(container, Messages.ColumnTargetCurrency,
                            "targetCurrencyCode", false); //$NON-NLS-1$
            targetCurrencyCode.getCombo().setToolTipText(Messages.ColumnTargetCurrencyToolTip);
        }

        if (!isExchangeRate)
        {
            isin = bindings.bindISINInput(container, Messages.ColumnISIN, "isin"); //$NON-NLS-1$
            isin.setEnabled(!isSyncedOnline);
        }

        bindings.bindStringInput(container, Messages.ColumnTicker, "tickerSymbol", SWT.NONE, 12); //$NON-NLS-1$

        if (!isExchangeRate)
        {
            wkn = bindings.bindStringInput(container, Messages.ColumnWKN, "wkn", SWT.NONE, 12); //$NON-NLS-1$
            wkn.setEnabled(!isSyncedOnline);

            ComboViewer calendar = bindings.bindCalendarCombo(container, Messages.LabelSecurityCalendar, "calendar"); //$NON-NLS-1$
            calendar.getCombo().setToolTipText(Messages.LabelSecurityCalendarToolTip);
        }

        Control control = bindings.bindBooleanInput(container, Messages.ColumnRetired, "retired"); //$NON-NLS-1$

        int margin = 2;
        Image info = Images.INFO.image();
        Rectangle bounds = info.getBounds();

        GridDataFactory.fillDefaults().indent(bounds.width + margin, 0).applyTo(control);

        ControlDecoration deco = new ControlDecoration(control, SWT.CENTER | SWT.LEFT);
        deco.setDescriptionText(Messages.MsgInfoRetiredSecurities);
        deco.setImage(info);
        deco.setMarginWidth(margin);
        deco.show();

        Text valueNote = bindings.bindStringInput(container, Messages.ColumnNote, "note", //$NON-NLS-1$
                        SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL, SWT.DEFAULT);
        GridDataFactory.fillDefaults().grab(true, false).hint(SWT.DEFAULT, SWTHelper.lineHeight(valueNote) * 4)
                        .applyTo(valueNote);
    }
}

@@ -815,24 +815,70 @@
 dingHelper;%0A
+import name.abuchen.portfolio.ui.util.Colors;%0A
 import name.
@@ -915,16 +915,16 @@
 topAPI;%0A
-
 import n
@@ -2826,38 +2826,39 @@
    isin.setE
-n
+dit
 able
-d
 (true);%0A
             
@@ -2837,32 +2837,74 @@
 Editable(true);%0A
+                isin.setBackground(null);%0A
                 
@@ -2915,22 +2915,23 @@
 setE
-n
+dit
 able
-d
 (true);%0A
     
@@ -2918,32 +2918,73 @@
 Editable(true);%0A
+                wkn.setBackground(null);%0A
                 
@@ -4274,32 +4274,82 @@
 ; //$NON-NLS-1$%0A
+            if (isSyncedOnline)%0A            %7B%0A    
             isin
@@ -4357,32 +4357,109 @@
 setE
-n
+dit
 able
-d(!isSyncedOnline);
+(false);%0A                isin.setBackground(Colors.SIDEBAR_BACKGROUND_SELECTED);%0A            %7D
 %0A   
@@ -4720,32 +4720,82 @@
 ; //$NON-NLS-1$%0A
+            if (isSyncedOnline)%0A            %7B%0A    
             wkn.
@@ -4802,32 +4802,108 @@
 setE
-n
+dit
 able
-d(!isSyncedOnline);
+(false);%0A                wkn.setBackground(Colors.SIDEBAR_BACKGROUND_SELECTED);%0A            %7D
 %0A%0A  

Allow copying of ISIN and WKN even if the security is linked