package com.yutou.jianr_mg.Databases;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.yutou.jianr_mg.Application;
import com.yutou.jianr_mg.Data.MGamePackname;
import com.yutou.jianr_mg.Data.TMod;

import java.util.ArrayList;
import java.util.List;

public class ModDatabase extends SQLiteOpenHelper {
    private static ModDatabase database;
    private SQLiteDatabase writeDatabase, readDatabase;
    private static String TAB_MOD_INSTALL = "t_mod_install";
    private static String TAB_MOD_LIST="t_mod_list";
    public static ModDatabase init(){
        if(database==null){
            database=new ModDatabase();
            database.writeDatabase=database.getWritableDatabase();
            database.readDatabase=database.getReadableDatabase();
        }
        return database;
    }
    private ModDatabase() {
        super(Application.application, "mg_mod_db", null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TAB_MOD_INSTALL + " (_id integer primary key,id integer,modName verchar(50),isInstall integer,installClient verchar(255),clientName verchar(255),isRoot integer,modVersion integer)";
        String t_2="create table "+TAB_MOD_LIST+" (_id integer primary key,id integer, modName verchar(50),info verchar(255),icon verchar(255),modVersion integer)";

        db.execSQL(sql);
        db.execSQL(t_2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }



    public void addInstallMod(TMod mod, MGamePackname packname){
        ContentValues values=new ContentValues();
        values.put("id",mod.getId());
        values.put("modName",mod.getTitle());
        values.put("isInstall",1);
        values.put("installClient",packname.getPackname());
        values.put("modVersion",mod.getModversion());
        values.put("clientName",packname.getZhname());
        values.put("isRoot",packname.isRoot()?1:0);
        if(isInstallMod(mod,packname.getPackname(),false)){
            writeDatabase.update(TAB_MOD_INSTALL,values,"id=? and installClient=?",new String[]{mod.getId()+"",packname.getPackname()});
        }else {
            writeDatabase.insert(TAB_MOD_INSTALL, null, values);
        }
    }
    public boolean isInstallMod(TMod mod){
        return  isInstallMod(mod,null,true);
    }
    private boolean isInstallMod(TMod mod,String packName, boolean isInstall){
        Cursor cursor;
        String sql;
        String[] tmp;
        if(packName==null) {
            sql = "id=? and modVersion=? and isInstall=?";
            tmp=new String[]{mod.getId()+"",mod.getModversion()+"","1"};
        }else{
            sql="id=? and modVersion=? and installClient=? and isInstall=?";
            tmp=new String[]{mod.getId()+"",packName,mod.getModversion()+"","1"};
        }
        if(isInstall){
            cursor=readDatabase.query(TAB_MOD_INSTALL,new String[]{"*"},sql,tmp,null,null,null);
        }else{
            cursor=readDatabase.query(TAB_MOD_INSTALL,new String[]{"*"},"id=? and modVersion=? and installClient=?",new String[]{mod.getId()+"",mod.getModversion()+"",packName},null,null,null);
        }

        boolean flag=cursor.getCount() != 0;
        cursor.close();
        return flag;
    }
    public void backInstaMod(TMod mod,String packName){
        ContentValues values=new ContentValues();
        values.put("isInstall",0);
        writeDatabase.update(TAB_MOD_INSTALL,values,"id=? and modVersion=? and installClient=?",new String[]{mod.getId()+"",mod.getModversion()+"",packName});
    }
    public List<MGamePackname> getInstallClients(TMod mod){
        List<MGamePackname> installClients = new ArrayList<>();
        try {
            Cursor cursor = readDatabase.query(TAB_MOD_INSTALL, new String[]{"*"}, "id=? and modVersion=?", new String[]{mod.getId() + "", mod.getModversion() + ""}, null, null, null);
            cursor.moveToFirst();
            do {
                MGamePackname packname = new MGamePackname();
                packname.setPackname(cursor.getString(cursor.getColumnIndex("installClient")));
                packname.setZhname(cursor.getString(cursor.getColumnIndex("clientName")));
                packname.setRoot(cursor.getInt(cursor.getColumnIndex("isRoot")) == 1);
                installClients.add(packname);
            } while (cursor.moveToNext());
            cursor.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  installClients;
    }
    public void downloadMod(TMod mod){
        ContentValues values=new ContentValues();
        values.put("id",mod.getId());
        values.put("modName",mod.getTitle());
        values.put("info",mod.getInfo());
        values.put("modVersion",mod.getModversion());
        values.put("icon",mod.getIcon());
        if(!isDownloadMod(mod)){
            writeDatabase.insert(TAB_MOD_LIST,null,values);
        }else{
            deleteMod(mod);
            downloadMod(mod);
        }
    }
    public void deleteMod(TMod mod){
        writeDatabase.delete(TAB_MOD_LIST,"id=?",new String[]{mod.getId()+""});
    }
    public boolean isDownloadMod(TMod mod){
        Cursor cursor=readDatabase.query(TAB_MOD_LIST,new String[]{"*"},"id=?",new String[]{mod.getId()+""},null,null,null);
        boolean flag=cursor.getCount()>0;
        cursor.close();
        return flag;
    }
    public List<TMod> getDownloadModIds(){
        List<TMod> list=new ArrayList<>();
        Cursor cursor=readDatabase.query(TAB_MOD_LIST,new String[]{"*"},null,null,null,null,null);
        if(cursor!=null){
            while (cursor.moveToNext()) {
                TMod mod=new TMod();
                mod.setId(cursor.getInt(cursor.getColumnIndex("id")));
                mod.setTitle(cursor.getString(cursor.getColumnIndex("modName")));
                mod.setInfo(cursor.getString(cursor.getColumnIndex("info")));
                mod.setModversion(cursor.getInt(cursor.getColumnIndex("modVersion")));
                mod.setIcon(cursor.getString(cursor.getColumnIndex("icon")));
                list.add(mod);
            }
            cursor.close();
        }
        System.out.println(list.size());
        return list;
    }
    public int getModVersion(TMod mod){
            Cursor cursor=readDatabase.query(TAB_MOD_LIST,new String[]{"*"},"id=?",new String[]{mod.getId()+""},null,null,null);
            int version=-1;
            try {
                cursor.moveToFirst();
                version=cursor.getInt(cursor.getColumnIndex("modVersion"));
            }catch (Exception e){
                e.printStackTrace();
            }
            cursor.close();
            return  version;
    }
}

