package com.sstf.tool;

import com.brilliance.esb.core.util.CryptoUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;

public class EncrytTool extends JFrame  {
    JPanel center =new JPanel();
    public EncrytTool()throws IOException {
        init();
        this.setVisible(true);
    }
    private void init(){
        this.setSize(800,600);
        this.setTitle("sql加密");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);           //设置为空布局
        this.setResizable(false);

//        JLabel jl=new JLabel();
//

        Font font=new Font("微软雅黑",Font.BOLD,15);
        JTextArea  jl= new JTextArea();
        jl.setBounds(10,300,600,300);
       // let the text pane know this is what you want
        jl.setEditable(false); // as before
//        jl.setBackground(null); // this is the same as a JLabel
        jl.setBorder(null); // remove the border
        jl.setLineWrap(true);
        jl.setFont(font);

//        jl.set
        JTextArea jtf =new JTextArea();   //文本框
        jtf.setBounds(10,10,600,200);
        jtf.setLineWrap(true);
        jtf.setWrapStyleWord(true);
        jtf.setFont(font);
        JButton jb=new JButton("加密");        //鼠标单击监听器
        jb.setBounds(610,10,60,30);
        jb.addActionListener(
                (ActionListener) ->{
                    String text =jtf.getText();
                    List<String> lines = Arrays.asList(text.split(";"));

                    List<String> allLines = new ArrayList<>();

                    lines.stream().filter(line->!line.trim().isEmpty())
                            .forEach(line->{
                                String newline = line.replace('\uFEFF',' ').trim();
                                String aesLine = CryptoUtil.encrypt(newline,"AES");
                                allLines.add(newline);
                                allLines.add(aesLine);
                            });


                    StringJoiner sj = new StringJoiner("\r\n");
                    AtomicInteger index = new AtomicInteger();

                    allLines.forEach((line)->{
                        System.out.println(line);
                        System.out.println();
                        sj.add(line);
                        sj.add("");
                        index.incrementAndGet();

                        if(index.intValue() % 2 == 0){
                            String hline="--------------------------------------------------------------------------------------------------";
                            System.out.println(hline);
                            sj.add(hline);
                        }

                    });
                    jl.setText( sj.toString());
                });
//        jtf.addActionListener((ActionEvent e)->{    //常用键enter  用addActionListener
//            jb.doClick();
//        });

//
        JScrollPane jpl=  new JScrollPane(jl);
        jpl.setBounds(10,300,600,300);
        JScrollPane jpf=  new JScrollPane(jtf);
        jpf.setBounds(10,10,600,200);
//
        this.add(jpl);
        this.add(jpf);
        this.add(jb);



    }

    public static void main(String[] args) {
        try {
            EncrytTool encrytTool=new EncrytTool();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
