package glossary.gui;

import glossary.engine.facade.WritingTestFacade;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Created by Papi on 6. 7. 2017.
 */
public class WritingTestPanel extends JPanel{

    private JTextField translation;
    private JLabel  word;
    public WritingTestPanel(LayoutManager layout) {
        super(layout);
    }

    public void init(){
        word = new JLabel("WORD");
        JLabel lAnswer = new JLabel("Your answer is:");
        translation = new JTextField("CUSTOM TRANSLATION");
        JButton buttonNext = new JButton("Next");
        Dimension dim;
        Rectangle rec;


        dim = new Dimension(400, 150);
        rec = new Rectangle(100, 100, dim.width, dim.height);
        word.setBounds(rec);
        setFontSize(word, dim);

        dim = new Dimension(400, 100);
        rec = new Rectangle(100,300 , dim.width, dim.height);
        setFontSize(lAnswer, dim);
        lAnswer.setBounds(rec);



        dim = new Dimension(400, 100);
        rec = new Rectangle(100, 450, dim.width, dim.height);
        translation.setBounds(rec);
        add(word);
        add(translation);
        add(lAnswer);

        buttonNext.addActionListener((ActionEvent e) -> {

        });
    }

    public void startTest(WritingTestFacade facade){

    }

    private void setFontSize(JLabel label, Dimension labelSize) {
        Font labelFont = label.getFont();
        String labelText = label.getText();
        int componentWidth;
        int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);
        if (labelSize == null) {
            componentWidth = label.getWidth();
        } else {
            componentWidth = labelSize.width;
        }
        double widthRatio = (double) componentWidth / (double) stringWidth;
        int newFontSize = (int) (labelFont.getSize() * widthRatio);
        int componentHeight;
        if (labelSize == null) {
            componentHeight = label.getHeight();
        } else {
            componentHeight = labelSize.height;
        }
        int fontSizeToUse = Math.min(newFontSize, componentHeight);
        label.setFont(new Font("ComicSans", Font.PLAIN, fontSizeToUse));
    }
}

@@ -64,16 +64,51 @@
 tFacade;
+%0Aimport glossary.engine.model.Card;
 %0A%0Aimport
@@ -718,17 +718,17 @@
 n(400, 1
-5
+0
 0);%0A    
@@ -826,29 +826,53 @@
     
+word.
 setFont
-Size(word, dim
+(new Font(%22Arial%22, Font.BOLD, 30)
 );%0A%0A
@@ -891,39 +891,38 @@
 = new Dimension(
-4
+6
 00, 
-10
+8
 0);%0A        rec 
@@ -941,18 +941,18 @@
 gle(100,
-30
+24
 0 , dim.
@@ -983,32 +983,56 @@
     
-setFontSize(lAnswer, dim
+lAnswer.setFont(new Font(%22Arial%22, Font.BOLD, 20)
 );%0A 
@@ -1063,18 +1063,16 @@
 (rec);%0A%0A
-%0A%0A
         
@@ -1083,39 +1083,38 @@
 = new Dimension(
-4
+6
 00, 
-10
+5
 0);%0A        rec 
@@ -1134,17 +1134,17 @@
 le(100, 
-4
+3
 50, dim.
@@ -1190,32 +1190,170 @@
 .setBounds(rec);
+%0A%0A        dim = new Dimension(200,80);%0A        rec = new Rectangle(500, 400, dim.width, dim.height);%0A        buttonNext.setBounds(rec);%0A%0A%0A
 %0A        add(wor
@@ -1403,16 +1403,41 @@
 Answer);
+%0A        add(buttonNext);
 %0A%0A      
@@ -1488,16 +1488,39 @@
 e) -%3E %7B%0A
+        continueTest();
 %0A       
@@ -1522,33 +1522,32 @@
       %7D);%0A    %7D%0A
-%0A
     public void 
@@ -1587,161 +1587,39 @@
 e)%7B%0A
-%0A
     
-%7D%0A%0A
     
-private void setFontSize(JLabel label, Dimension labelSize) %7B%0A        Font labelFont = label.getFont();%0A        String labelText = label
+Card card = facade
 .get
-T
+N
 ext(
@@ -1633,738 +1633,172 @@
     
-int componentWidth;%0A        int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);%0A        if (labelSize == null) %7B%0A            componentWidth = label.getWidth();%0A        %7D else %7B%0A            componentWidth = labelSize.width;%0A        %7D%0A        double widthRatio = (double) componentWidth / (double) stringWidth
+word.setText(card.getWord1());%0A        translation.setText(%22%22)
 ;%0A    
- 
-   int newFontSize = (int) (labelFont.getSize() * widthRatio);%0A        int componentHeight;%0A        if (labelSize == null) %7B%0A            componentHeight = label.getHeight();%0A        %7D else %7B%0A            componentHeight = labelSize.height;%0A        %7D%0A        int fontSizeToUse = Math.min(newFontSize, componentHeight);%0A        label.setFont(new Font(%22ComicSans%22, Font.PLAIN, fontSizeToUse));
+%7D%0A%0A    public void continueTest()%7B%0A        String customAnswer = translation.getText();%0A%0A
 %0A    %7D%0A
+%0A%0A
 %7D%0A

Improve testing