package com.qy.ui;

import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class TestAdSrc {
    private JCheckBox checkBox;
    private JPanel panel1;
    private JLabel jLable;

    public TestAdSrc() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        JFrame frame = new JFrame("广告源分析工具1.0");
        frame.setContentPane(panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(420, 400);
        frame.setVisible(true);
        adSrcList();
        initView();
    }

    public void initView() {
        panel1.setTransferHandler(new TransferHandler() {
            @Override
            public boolean importData(JComponent comp, Transferable t) {
                try {
                    Object obj = t.getTransferData(DataFlavor.javaFileListFlavor);
                    File file = new File(obj.toString().substring(1, obj.toString().length() - 1));
                    System.out.println(file.getAbsolutePath());
                    if (!file.getName().endsWith(".apk")) {
                        new TextToDalog("警告", "并非一个apk文件:" + file.getAbsolutePath());
                        return true;
                    }
                    if (checkBox.isSelected()) {
                        completeAnalysis(file);
                    } else {
                        analysis(file);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }

            @Override
            public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
                return true;
            }
        });
    }

    private List<String> gdt, csj, bd, ow, up;

    private void adSrcList() {
        gdt = new ArrayList<>();
        csj = new ArrayList<>();
        bd = new ArrayList<>();
        ow = new ArrayList<>();
        up = new ArrayList<>();
        gdt.add("gdt_plugin");
        csj.add("libtobEmbedEncrypt.so");
        bd.add("bdxadsdk.jar");
        ow.add("OnewaySdk.jar");
        up.add("uniplayad_logo.png");
    }

    private void analysis(File file) {
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            String msg = "";
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                for (String s : gdt) {
                    if (entry.getName().contains(s)&&!msg.contains("广点通")) {
                        msg += "广点通 ";
                        break;
                    }
                }
                for (String s : bd) {
                    if (entry.getName().contains(s)&&!msg.contains("百青藤")) {
                        msg += "百青藤 ";
                        break;
                    }
                }
                for (String s : csj) {
                    if (entry.getName().contains(s)&&!msg.contains("穿山甲")) {
                        msg += "穿山甲 ";
                        break;
                    }
                }
                for (String s : ow) {
                    if (entry.getName().contains(s)&&!msg.contains("万维")) {
                        msg += "万维 ";
                        break;
                    }
                }
                for (String s : up) {
                    if (entry.getName().contains(s)&&!msg.contains("聚量")) {
                        msg += "聚量 ";
                        break;
                    }
                }
            }
            if(msg.equals("")){
                new TextToDalog("提示",file.getName()+"不包含已知广告源");
            }else{
                new TextToDalog("提示","已扫描到："+msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void completeAnalysis(File file) {

    }

    public static void main(String[] args) {
        new TestAdSrc();
    }
}
