package cn.com.agree;

import cn.com.agree.adore.ADORE;
import cn.com.agree.adore.AdoreDisplay;
import cn.com.agree.adore.event.ClickEvent;
import cn.com.agree.adore.layout.RowGridLayout;
import cn.com.agree.adore.layout.RowGridLayoutData;
import cn.com.agree.adore.widget.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * Created by weiwei01 on 2016-12-16.
 */
public class MainWindow {
    private Shell shell;
    private String salaryFile;
    private String emailFile;

    private Label sendLabel;

    protected MainWindow(){
        System.setProperty("org.eclipse.swt.browser.XULRunnerPath", (new File(".\\xulrunner31")).getAbsolutePath());
        Display display = Display.getDefault();
        shell = new Shell(display);

        shell.setSize(800, 600);
        shell.setText("工资单小工具");
        shell.setLayout(new FillLayout());
        initAdore(shell);
        createComponent();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    private void initAdore(Composite composite){
        int browser_type=SWT.MOZILLA;
        if(System.getProperty("os.name").equals("Linux")){
            browser_type=SWT.WEBKIT;
        }
        Browser browser = new Browser(composite, browser_type);
        browser.setJavascriptEnabled(true);
        XulrunnerAdoreBrowser adoreBrowser = new XulrunnerAdoreBrowser(browser);
        AdoreDisplay.init(adoreBrowser);
    }

    private void createComponent(){
        AdoreDisplay.getDefault().asyncExec(()-> {
            final RootComposite adoreshell = new RootComposite(AdoreDisplay.getCurrent(), ADORE.Normal);
            adoreshell.setLayout(new cn.com.agree.adore.layout.FillLayout());
            cn.com.agree.adore.widget.Composite composite = new cn.com.agree.adore.widget.Composite(adoreshell, ADORE.Normal);
            composite.setLayout(new RowGridLayout(4));
            Map<String, String> addoncss = new HashMap<>();
            addoncss.put("margin-top","15px");
            Text salaryFileText = new Text(composite, ADORE.Normal);
            salaryFileText.setName("工资清单");
            salaryFileText.setLayoutData(new RowGridLayoutData(0,7,7,7,1,1,1));
            Button selFileButton = new Button(composite, ADORE.Normal);
            selFileButton.setText("选择...");
            selFileButton.setCss(addoncss);
            selFileButton.setLayoutData(new RowGridLayoutData(0,2,2,2,0,0,0));
            selFileButton.addClickListener((ClickEvent clickEvent) ->{
                final CountDownLatch latch = new CountDownLatch(1);
                Display.getDefault().asyncExec(()-> {
                    FileDialog dialog = new FileDialog(shell, SWT.OPEN);
                    dialog.setText("选择工资表");
                    dialog.setFilterExtensions(new String[]{"*.xlsx", "*.xls"});
                    salaryFile = dialog.open();
                    latch.countDown();
                });
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                salaryFileText.setText(salaryFile);
            });

            Text nameFileText = new Text(composite, ADORE.Normal);
            nameFileText.setName("邮箱列表");
            nameFileText.setLayoutData(new RowGridLayoutData(1,7,7,7,1,1,1));
            Button selMailButton = new Button(composite, ADORE.Normal);
            selMailButton.setText("选择...");
            selMailButton.setCss(addoncss);
            selMailButton.setLayoutData(new RowGridLayoutData(1,2,2,2,0,0,0));
            selMailButton.addClickListener((ClickEvent clickEvent) ->{

                final CountDownLatch latch = new CountDownLatch(1);
                Display.getDefault().asyncExec(()->{
                    FileDialog dialog = new FileDialog(shell,SWT.OPEN);
                    dialog.setText("选择工资表");
                    dialog.setFilterExtensions(new String[]{"*.txt"});
                    emailFile = dialog.open();
                    latch.countDown();
                });
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                nameFileText.setText(emailFile);
            });

            Button sendButton = new Button(composite, ADORE.Normal);
            sendButton.setText("发送！");
            sendButton.setBackgroundColor("rgb(253,56,40)");
            sendButton.setLayoutData(new RowGridLayoutData(2,3,3,3,4,4,4));
            sendButton.addClickListener((ClickEvent clickEvent) ->{
                sendLabel.setText("开始发送...");
                Runnable r = ()->{
                    try {
                        MailTool tool = new MailTool(salaryFile, emailFile);
                        tool.addMailListener((String message)-> {
                            AdoreDisplay.getDefault().asyncExec(()-> {
                                sendLabel.setText(message);
                            });
                        });
                        tool.sendMail();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
                new Thread(r).start();
            });

            sendLabel = new Label(composite, ADORE.Normal);
            sendLabel.setLayoutData(new RowGridLayoutData(3,5,5,5,1,1,1));
            sendLabel.setFontSize(14);
            adoreshell.layout();
            adoreshell.refresh();
        });
    }

    public static void main(String[] args){
        File file =new File("./");
        System.out.println(file.getAbsolutePath());
        new MainWindow();

    }
}
