package com.unibuy.smartdevice.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "mydata.db";
    private static final int DATABASE_VERSION = 1;

    public enum column {
        COLUMN_ID("INTEGER PRIMARY KEY");
        private String type;
        column(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            if (this.name().equals("TABLE")) {
                int index = this.getClass().getName().indexOf('$');
                return this.getClass().getName().substring(index + 1);
            } else {
                return this.name().toLowerCase();
            }
        }

        public String getCreateSQL(String end) {
            if (this.name().equals("TABLE")) {
                return this.getName() + end;
            } else {
                return this.name().toLowerCase() + " " + getType() + end;
            }
        }
    }

    public enum settinglogs {
        TABLE("settinglogs"),
        STATUS("TEXT NOT NULL")
        ;

        private String type;
        settinglogs(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            if (this.name().equals("TABLE")) {
                return this.getType();
            } else {
                return this.name().toLowerCase();
            }
        }

        public String getCreateSQL(String end) {
            if (this.name().equals("TABLE")) {
                return this.getName() + end;
            } else {
                return this.name().toLowerCase() + " " + getType() + end;
            }
        }
    }

    public enum settings {
        TABLE("settings"),
        SETTING_ID("TEXT NOT NULL"),
        DATA1("TEXT"),
        DATA2("TEXT"),
        DATA3("TEXT"),
        DATA4("TEXT"),
        DATA5("TEXT"),
        DATA6("TEXT"),
        DATA7("TEXT"),
        DATA8("TEXT"),
        DATA9("TEXT"),
        DATA10("TEXT")
        ;

        private String type;
        settings(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            if (this.name().equals("TABLE")) {
                return this.getType();
            } else {
                return this.name().toLowerCase();
            }
        }

        public String getCreateSQL(String end) {
            if (this.name().equals("TABLE")) {
                return this.getName() + end;
            } else {
                return this.name().toLowerCase() + " " + getType() + end;
            }
        }
    }

    public enum products {
        TABLE("products"),
        PRODUCT_ID("TEXT NOT NULL"),
        PRODUCT_IMG("TEXT NOT NULL"),
        PRODUCT_NAME("TEXT NOT NULL"),
        MACHINE_PRICE("INTEGER NOT NULL"),
        SELLING_PRICE("INTEGER NOT NULL"),
        MEMBER_PRICE("INTEGER NOT NULL"),
        SLOT_UPPER_LIMIT("INTEGER NOT NULL"),
        MARKETING_PLAN("TEXT NOT NULL"),
        MATERIAL_CODE("TEXT NOT NULL"),
        PRODUCT_NAME_EN("TEXT NOT NULL"),
        PRODUCT_NAME_JP("TEXT NOT NULL")
        ;

        private String type;
        products(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            if (this.name().equals("TABLE")) {
                return this.getType();
            } else {
                return this.name().toLowerCase();
            }
        }

        public String getCreateSQL(String end) {
            if (this.name().equals("TABLE")) {
                return this.getName() + end;
            } else {
                return this.name().toLowerCase() + " " + getType() + end;
            }
        }
    }

    /*
        this.slot = slot;
        this.product = product;
        this.count = count;
        this.lock = false;
     */
    public enum slot {
        TABLE("slot"),
        FIELD("TEXT NOT NULL"),
        SLOT("INTEGER NOT NULL"),
        PRODUCT_ID("TEXT NOT NULL"),
        COUNT("INTEGER NOT NULL"),
        LOCK("TINYINT NOT NULL"),
        OTHER_DATA("TEXT"),
        ;

        private String type;
        slot(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            if (this.name().equals("TABLE")) {
                return this.getType();
            } else {
                return this.name().toLowerCase();
            }
        }

        public String getCreateSQL(String end) {
            if (this.name().equals("TABLE")) {
                return this.getName() + end;
            } else {
                return this.name().toLowerCase() + " " + getType() + end;
            }
        }
    }

    public enum memo {
        TABLE("memo"),
        FIELD("TEXT NOT NULL"),
        DATA("TEXT NOT NULL"),
        OTHER_DATA("TEXT"),
        ;

        private String type;
        memo(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            if (this.name().equals("TABLE")) {
                return this.getType();
            } else {
                return this.name().toLowerCase();
            }
        }

        public String getCreateSQL(String end) {
            if (this.name().equals("TABLE")) {
                return this.getName() + end;
            } else {
                return this.name().toLowerCase() + " " + getType() + end;
            }
        }
    }

    // 建表語句
    private static final String CREATE_TABLE_SETTINGLOGS =
            "CREATE TABLE " + settinglogs.TABLE.getCreateSQL("(") +
                    column.COLUMN_ID.getCreateSQL(", ") +
                    settinglogs.STATUS.getCreateSQL(");");
    private static final String CREATE_TABLE_SETTINGS =
            "CREATE TABLE " + settings.TABLE.getCreateSQL("(") +
                    column.COLUMN_ID.getCreateSQL(", ") +
                    settings.SETTING_ID.getCreateSQL(", ") +
                    settings.DATA1.getCreateSQL(", ") +
                    settings.DATA2.getCreateSQL(", ") +
                    settings.DATA3.getCreateSQL(", ") +
                    settings.DATA4.getCreateSQL(", ") +
                    settings.DATA5.getCreateSQL(", ") +
                    settings.DATA6.getCreateSQL(", ") +
                    settings.DATA7.getCreateSQL(", ") +
                    settings.DATA8.getCreateSQL(", ") +
                    settings.DATA9.getCreateSQL(", ") +
                    settings.DATA10.getCreateSQL(");");
    private static final String CREATE_TABLE_PRODUCTS =
            "CREATE TABLE " + products.TABLE.getCreateSQL("(") +
                    column.COLUMN_ID.getCreateSQL(", ") +
                    products.PRODUCT_ID.getCreateSQL(", ") +
                    products.PRODUCT_IMG.getCreateSQL(", ") +
                    products.PRODUCT_NAME.getCreateSQL(", ") +
                    products.MACHINE_PRICE.getCreateSQL(", ") +
                    products.SELLING_PRICE.getCreateSQL(", ") +
                    products.MEMBER_PRICE.getCreateSQL(", ") +
                    products.SLOT_UPPER_LIMIT.getCreateSQL(", ") +
                    products.MARKETING_PLAN.getCreateSQL(", ") +
                    products.MATERIAL_CODE.getCreateSQL(", ") +
                    products.PRODUCT_NAME_EN.getCreateSQL(", ") +
                    products.PRODUCT_NAME_JP.getCreateSQL(");");

    private static final String CREATE_TABLE_SLOT =
            "CREATE TABLE " + slot.TABLE.getCreateSQL("(") +
                    column.COLUMN_ID.getCreateSQL(", ") +
                    slot.FIELD.getCreateSQL(", ") +
                    slot.SLOT.getCreateSQL(", ") +
                    slot.PRODUCT_ID.getCreateSQL(", ") +
                    slot.COUNT.getCreateSQL(", ") +
                    slot.LOCK.getCreateSQL(", ") +
                    slot.OTHER_DATA.getCreateSQL(");")
            ;

    private static final String CREATE_TABLE_MEMO =
            "CREATE TABLE " + memo.TABLE.getCreateSQL("(") +
                    column.COLUMN_ID.getCreateSQL(", ") +
                    memo.FIELD.getCreateSQL(", ") +
                    memo.DATA.getCreateSQL(", ") +
                    memo.OTHER_DATA.getCreateSQL(");")
            ;


    public void exec(SQLiteDatabase db, String sqlString) {
        Log.i("", "sqlString:" + sqlString);
        db.execSQL(sqlString);
    }

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        exec(db, CREATE_TABLE_SETTINGLOGS);
        exec(db, CREATE_TABLE_SETTINGS);
        exec(db, CREATE_TABLE_PRODUCTS);
        exec(db, CREATE_TABLE_SLOT);
        exec(db, CREATE_TABLE_MEMO);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        exec(db, "DROP TABLE IF EXISTS " + settinglogs.TABLE.getName());
        exec(db, "DROP TABLE IF EXISTS " + settings.TABLE.getName());
        exec(db, "DROP TABLE IF EXISTS " + products.TABLE.getName());
        exec(db, "DROP TABLE IF EXISTS " + slot.TABLE.getName());
        exec(db, "DROP TABLE IF EXISTS " + memo.TABLE.getName());
        onCreate(db);
    }
}

