package xyz.wbsite.dbtool.javafx.manger.callable;

import xyz.wbsite.dbtool.javafx.manger.FreeMarkerManager;
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
import xyz.wbsite.dbtool.javafx.manger.ProjectManager;
import xyz.wbsite.dbtool.javafx.po.AbstractDBmapper;
import xyz.wbsite.dbtool.javafx.po.AndroidOption;
import xyz.wbsite.dbtool.javafx.tool.Tool;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;

import static xyz.wbsite.dbtool.javafx.tool.Tool.clear;

public class AndroidCallable implements Callable {

    private Tool tool = new Tool();
    private String path;
    private AndroidOption option;

    private FreeMarkerManager freeMarkerManager;

    public AndroidCallable(@NotNull String path, AndroidOption option) {
        this.path = path;
        this.option = option;
        this.freeMarkerManager = ManagerFactory.getFreeMarkerManager();
    }

    private AbstractDBmapper dBmapper;

    public Boolean call() throws Exception {
        File project = new File(path, option.projectName);
        if (!project.exists()) {
            project.mkdirs();
        } else {
            clear(project);
        }

        dBmapper = ProjectManager.dBmapper;

        // 目录生成
        File app = new File(project, "app");
        app.mkdirs();
        File src = new File(app, "src");
        src.mkdirs();
        File main = new File(src, "main");
        main.mkdirs();
        File java = new File(main, "java");
        java.mkdirs();
        File res = new File(main, "res");
        res.mkdirs();
        File domain = new File(java.getAbsolutePath() + File.separator + option.domain.replaceAll("\\.", Matcher.quoteReplacement(File.separator)));
        domain.mkdirs();
        File base = new File(domain, "base");
        base.mkdirs();
        File activity = new File(base, "activity");
        activity.mkdirs();
        File service = new File(base, "service");
        service.mkdirs();
        File util = new File(base, "util");
        util.mkdirs();
        File fragment = new File(domain, "fragment");
        fragment.mkdirs();
        File drawable = new File(res, "drawable");
        drawable.mkdirs();
        File layout = new File(res, "layout");
        layout.mkdirs();
        File mipmap_hdpi = new File(res, "mipmap-hdpi");
        mipmap_hdpi.mkdirs();
        File mipmap_mdpi = new File(res, "mipmap-mdpi");
        mipmap_mdpi.mkdirs();
        File mipmap_xhdpi = new File(res, "mipmap-xhdpi");
        mipmap_xhdpi.mkdirs();
        File mipmap_xxhdpi = new File(res, "mipmap-xxhdpi");
        mipmap_xxhdpi.mkdirs();
        File mipmap_xxxhdpi = new File(res, "mipmap-xxxhdpi");
        mipmap_xxxhdpi.mkdirs();
        File values = new File(res, "values");
        values.mkdirs();

        {
            Tool.outputResource("Android/.gitignore", new File(project, ".gitignore"));
            Tool.outputResource("Android/build.gradles", new File(project, "build.gradle"));
            Tool.outputResource("Android/settings.gradles", new File(project, "settings.gradle"));
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("package", option.packages);
            ctx.put("qrcode", option.qrcode);
            ctx.put("fileselect", option.fileselect);
            freeMarkerManager.outputTemp(new File(app, "build.gradle"), "Android/app/build.gradles", ctx);
            Tool.outputResource("Android/app/proguard-rules.pro", new File(app, "proguard-rules.pro"));
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("package", option.packages);
            ctx.put("domain", option.domain);
            ctx.put("qrcode", option.qrcode);
            ctx.put("fileselect", option.fileselect);
            freeMarkerManager.outputTemp(new File(main, "AndroidManifest.xml"), "Android/app/src/main/AndroidManifest.xml", ctx);
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("package", option.packages);
            ctx.put("domain", option.domain);
            freeMarkerManager.outputTemp(new File(base, "BaseActivity.java"), "Android/app/src/main/java/base/BaseActivity.java", ctx);
            freeMarkerManager.outputTemp(new File(base, "BaseFragment.java"), "Android/app/src/main/java/base/BaseFragment.java", ctx);
            freeMarkerManager.outputTemp(new File(base, "BaseSPAActivity.java"), "Android/app/src/main/java/base/BaseSPAActivity.java", ctx);
            freeMarkerManager.outputTemp(new File(base, "BaseSPAFragment.java"), "Android/app/src/main/java/base/BaseSPAFragment.java", ctx);
            freeMarkerManager.outputTemp(new File(base, "BaseSPATakePhotoFragment.java"), "Android/app/src/main/java/base/BaseSPATakePhotoFragment.java", ctx);
            freeMarkerManager.outputTemp(new File(base, "IActivityResult.java"), "Android/app/src/main/java/base/IActivityResult.java", ctx);

            freeMarkerManager.outputTemp(new File(activity, "TakePhotoActivity.java"), "Android/app/src/main/java/base/activity/TakePhotoActivity.java", ctx);
            if (option.fileselect) {
                freeMarkerManager.outputTemp(new File(activity, "FilePickerActivity.java"), "Android/app/src/main/java/base/activity/FilePickerActivity.java", ctx);
            }
            if (option.qrcode) {
                freeMarkerManager.outputTemp(new File(activity, "QRcodeActivity.java"), "Android/app/src/main/java/base/activity/QRcodeActivity.java", ctx);
                freeMarkerManager.outputTemp(new File(activity, "QRcodeScanActivity.java"), "Android/app/src/main/java/base/activity/QRcodeScanActivity.java", ctx);
            }

            freeMarkerManager.outputTemp(new File(service, "RestartService.java"), "Android/app/src/main/java/base/service/RestartService.java", ctx);

            freeMarkerManager.outputTemp(new File(util, "AnimationUtil.java"), "Android/app/src/main/java/base/util/AnimationUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "ApkUtil.java"), "Android/app/src/main/java/base/util/ApkUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "Base64Util.java"), "Android/app/src/main/java/base/util/Base64Util.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "BitmapUtil.java"), "Android/app/src/main/java/base/util/BitmapUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "BluetoothUtil.java"), "Android/app/src/main/java/base/util/BluetoothUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "Consant.java"), "Android/app/src/main/java/base/util/Consant.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "DataBaseUtil.java"), "Android/app/src/main/java/base/util/DataBaseUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "DensityUtil.java"), "Android/app/src/main/java/base/util/DensityUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "DESUtil.java"), "Android/app/src/main/java/base/util/DESUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "DialogUtil.java"), "Android/app/src/main/java/base/util/DialogUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "FastBlur.java"), "Android/app/src/main/java/base/util/FastBlur.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "FileUtil.java"), "Android/app/src/main/java/base/util/FileUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "GPSUtil.java"), "Android/app/src/main/java/base/util/GPSUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "IdCardUtil.java"), "Android/app/src/main/java/base/util/IdCardUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "Logger.java"), "Android/app/src/main/java/base/util/Logger.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "NetUtil.java"), "Android/app/src/main/java/base/util/NetUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "RSAUtil.java"), "Android/app/src/main/java/base/util/RSAUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "SizeUtil.java"), "Android/app/src/main/java/base/util/SizeUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "SPUtil.java"), "Android/app/src/main/java/base/util/SPUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "StorageUtil.java"), "Android/app/src/main/java/base/util/StorageUtil.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "Tasker.java"), "Android/app/src/main/java/base/util/Tasker.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "Toaster.java"), "Android/app/src/main/java/base/util/Toaster.java", ctx);
            freeMarkerManager.outputTemp(new File(util, "UriUtil.java"), "Android/app/src/main/java/base/util/UriUtil.java", ctx);
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("package", option.packages);
            ctx.put("domain", option.domain);
            ctx.put("webview", option.webview);
            freeMarkerManager.outputTemp(new File(domain, "WBUIApplication.java"), "Android/app/src/main/java/WBUIApplication.java", ctx);
            freeMarkerManager.outputTemp(new File(domain, "WBUIMainActivity.java"), "Android/app/src/main/java/WBUIMainActivity.java", ctx);
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("package", option.packages);
            ctx.put("domain", option.domain);
            if (option.webview) {
                freeMarkerManager.outputTemp(new File(fragment, "WebViewFragment.java"), "Android/app/src/main/java/fragment/WebViewFragment.java", ctx);
            } else {
                freeMarkerManager.outputTemp(new File(fragment, "MainFragment.java"), "Android/app/src/main/java/fragment/MainFragment.java", ctx);
            }
        }

        {
            Tool.outputResource("Android/app/src/main/res/drawable/background.xml", new File(drawable, "background.xml"));

            if (option.qrcode) {
                Tool.outputResource("Android/app/src/main/res/layout/activity_qrcode_scan.xml", new File(layout, "activity_qrcode_scan.xml"));
            }
            if (option.webview) {
                Tool.outputResource("Android/app/src/main/res/layout/fragment_webview.xml", new File(layout, "fragment_webview.xml"));
            } else {
                Tool.outputResource("Android/app/src/main/res/layout/fragment_main.xml", new File(layout, "fragment_main.xml"));
            }

            Tool.outputResource("Android/app/src/main/res/mipmap-mdpi/ic_launcher.png", new File(mipmap_mdpi, "ic_launcher.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-mdpi/ic_launcher.png", new File(mipmap_mdpi, "ic_launcher_round.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-hdpi/ic_launcher.png", new File(mipmap_hdpi, "ic_launcher.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-hdpi/ic_launcher.png", new File(mipmap_hdpi, "ic_launcher_round.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-xhdpi/ic_launcher.png", new File(mipmap_xhdpi, "ic_launcher.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-xhdpi/ic_launcher.png", new File(mipmap_xhdpi, "ic_launcher_round.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png", new File(mipmap_xxhdpi, "ic_launcher.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png", new File(mipmap_xxhdpi, "ic_launcher_round.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png", new File(mipmap_xxxhdpi, "ic_launcher.png"));
            Tool.outputResource("Android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png", new File(mipmap_xxxhdpi, "ic_launcher_round.png"));

            Tool.outputResource("Android/app/src/main/res/values/attrs.xml", new File(values, "attrs.xml"));
            Tool.outputResource("Android/app/src/main/res/values/colors.xml", new File(values, "colors.xml"));
            Tool.outputResource("Android/app/src/main/res/values/dimens.xml", new File(values, "dimens.xml"));
            Tool.outputResource("Android/app/src/main/res/values/ids.xml", new File(values, "ids.xml"));
            Tool.outputResource("Android/app/src/main/res/values/strings.xml", new File(values, "strings.xml"));
            Tool.outputResource("Android/app/src/main/res/values/styles.xml", new File(values, "styles.xml"));
        }

        System.out.println("finish");
        return true;
    }


    public class Method {
        private String request;
        private String stringMethod;
        private String manager;
        private String method;
        private String target;

        public String getRequest() {
            return request;
        }

        public void setRequest(String request) {
            this.request = request;
        }

        public String getTarget() {
            return target;
        }

        public void setTarget(String target) {
            this.target = target;
        }

        public String getStringMethod() {
            return stringMethod;
        }

        public void setStringMethod(String stringMethod) {
            this.stringMethod = stringMethod;
        }

        public String getManager() {
            return manager;
        }

        public void setManager(String manager) {
            this.manager = manager;
        }

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }
    }
}
