package com.oo.seex.oo_statistics_lib.utils.utdid.core;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;

import com.oo.seex.oo_statistics_lib.utils.utdid.utils.StringUtils;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class PersistentConfiguration {
    private static final String KEY_TIMESTAMP = "t";
    private static final String KEY_TIMESTAMP2 = "t2";
    private String mConfigName = "";
    private String mFolderName = "";
    private boolean mCanWrite = false;
    private SharedPreferences mSp = null;
    private MySharedPreferences mMySP = null;
    private Editor mEditor = null;
    private MySharedPreferences.MyEditor mMyEditor = null;
    private Context mContext = null;
    private TransactionXMLFile mTxf = null;
    private boolean mIsLessMode = false;

    public PersistentConfiguration(Context context, String folderName, String configName, boolean isSafety, boolean isLessMode) {
        boolean mIsSafety = isSafety;
        this.mIsLessMode = isLessMode;
        this.mConfigName = configName;
        this.mFolderName = folderName;
        this.mContext = context;
        long spT = 0L;
        long mySPT = 0L;
        if(context != null) {
            this.mSp = context.getSharedPreferences(configName, 0);
            spT = this.mSp.getLong("t", 0L);
        }

        String mountedProperty = Environment.getExternalStorageState();
        boolean mCanRead = false;
        if(StringUtils.isEmpty(mountedProperty)) {
            mCanRead = this.mCanWrite = false;
        } else if(mountedProperty.equals("mounted")) {
            mCanRead = this.mCanWrite = true;
        } else if(mountedProperty.equals("mounted_ro")) {
            mCanRead = true;
            this.mCanWrite = false;
        } else {
            mCanRead = this.mCanWrite = false;
        }

        if((mCanRead || this.mCanWrite) && context != null && !StringUtils.isEmpty(folderName)) {
            this.mTxf = this.getTransactionXMLFile(folderName);
            if(this.mTxf != null) {
                try {
                    this.mMySP = this.mTxf.getMySharedPreferences(configName, 0);
                    mySPT = this.mMySP.getLong("t", 0L);
                    if(!isLessMode) {
                        if(spT > mySPT) {
                            this.copySPToMySP(this.mSp, this.mMySP);
                            this.mMySP = this.mTxf.getMySharedPreferences(configName, 0);
                        } else if(spT < mySPT) {
                            this.copyMySPToSP(this.mMySP, this.mSp);
                            this.mSp = context.getSharedPreferences(configName, 0);
                        } else if(spT == mySPT) {
                            this.copySPToMySP(this.mSp, this.mMySP);
                            this.mMySP = this.mTxf.getMySharedPreferences(configName, 0);
                        }
                    } else {
                        spT = this.mSp.getLong("t2", 0L);
                        mySPT = this.mMySP.getLong("t2", 0L);
                        if(spT < mySPT && spT > 0L) {
                            this.copySPToMySP(this.mSp, this.mMySP);
                            this.mMySP = this.mTxf.getMySharedPreferences(configName, 0);
                        } else if(spT > mySPT && mySPT > 0L) {
                            this.copyMySPToSP(this.mMySP, this.mSp);
                            this.mSp = context.getSharedPreferences(configName, 0);
                        } else if(spT == 0L && mySPT > 0L) {
                            this.copyMySPToSP(this.mMySP, this.mSp);
                            this.mSp = context.getSharedPreferences(configName, 0);
                        } else if(mySPT == 0L && spT > 0L) {
                            this.copySPToMySP(this.mSp, this.mMySP);
                            this.mMySP = this.mTxf.getMySharedPreferences(configName, 0);
                        } else if(spT == mySPT) {
                            this.copySPToMySP(this.mSp, this.mMySP);
                            this.mMySP = this.mTxf.getMySharedPreferences(configName, 0);
                        }
                    }
                } catch (Exception var15) {
                    ;
                }
            }
        }

        if(spT != mySPT || spT == 0L && mySPT == 0L) {
            long timestamp = System.currentTimeMillis();
            if(!this.mIsLessMode || this.mIsLessMode && spT == 0L && mySPT == 0L) {
                if(this.mSp != null) {
                    Editor e = this.mSp.edit();
                    e.putLong("t2", timestamp);
                    e.commit();
                }

                try {
                    if(this.mMySP != null) {
                        MySharedPreferences.MyEditor e1 = this.mMySP.edit();
                        e1.putLong("t2", timestamp);
                        e1.commit();
                    }
                } catch (Exception var14) {
                    ;
                }
            }
        }

    }

    private TransactionXMLFile getTransactionXMLFile(String folderName) {
        File rootFolder = this.getRootFolder(folderName);
        if(rootFolder != null) {
            this.mTxf = new TransactionXMLFile(rootFolder.getAbsolutePath());
            return this.mTxf;
        } else {
            return null;
        }
    }

    private File getRootFolder(String folderName) {
        File sdCardFile = Environment.getExternalStorageDirectory();
        if(sdCardFile != null) {
            File rootFolder = new File(String.format("%s%s%s", new Object[]{sdCardFile.getAbsolutePath(), File.separator, folderName}));
            if(rootFolder != null && !rootFolder.exists()) {
                rootFolder.mkdirs();
            }

            return rootFolder;
        } else {
            return null;
        }
    }

    private void copySPToMySP(SharedPreferences sp1, MySharedPreferences sp2) {
        if(sp1 != null && sp2 != null) {
            MySharedPreferences.MyEditor myEditorTmp = sp2.edit();
            if(myEditorTmp != null) {
                myEditorTmp.clear();
                Iterator itorator = sp1.getAll().entrySet().iterator();

                while(itorator.hasNext()) {
                    Entry e = (Entry)itorator.next();
                    String key = (String)e.getKey();
                    Object value = e.getValue();
                    if(value instanceof String) {
                        myEditorTmp.putString(key, (String)value);
                    } else if(value instanceof Integer) {
                        myEditorTmp.putInt(key, ((Integer)value).intValue());
                    } else if(value instanceof Long) {
                        myEditorTmp.putLong(key, ((Long)value).longValue());
                    } else if(value instanceof Float) {
                        myEditorTmp.putFloat(key, ((Float)value).floatValue());
                    } else if(value instanceof Boolean) {
                        myEditorTmp.putBoolean(key, ((Boolean)value).booleanValue());
                    }
                }

                myEditorTmp.commit();
            }
        }

    }

    private void copyMySPToSP(MySharedPreferences sp1, SharedPreferences sp2) {
        if(sp1 != null && sp2 != null) {
            Editor editorTmp = sp2.edit();
            if(editorTmp != null) {
                editorTmp.clear();
                Iterator itorator = sp1.getAll().entrySet().iterator();

                while(itorator.hasNext()) {
                    Entry e = (Entry)itorator.next();
                    String key = (String)e.getKey();
                    Object value = e.getValue();
                    if(value instanceof String) {
                        editorTmp.putString(key, (String)value);
                    } else if(value instanceof Integer) {
                        editorTmp.putInt(key, ((Integer)value).intValue());
                    } else if(value instanceof Long) {
                        editorTmp.putLong(key, ((Long)value).longValue());
                    } else if(value instanceof Float) {
                        editorTmp.putFloat(key, ((Float)value).floatValue());
                    } else if(value instanceof Boolean) {
                        editorTmp.putBoolean(key, ((Boolean)value).booleanValue());
                    }
                }

                editorTmp.commit();
            }
        }

    }

    private boolean checkSDCardXMLFile() {
        if(this.mMySP != null) {
            boolean isExist = this.mMySP.checkFile();
            if(!isExist) {
                this.commit();
            }

            return isExist;
        } else {
            return false;
        }
    }

    private void initEditor() {
        if(this.mEditor == null && this.mSp != null) {
            this.mEditor = this.mSp.edit();
        }

        if(this.mCanWrite && this.mMyEditor == null && this.mMySP != null) {
            this.mMyEditor = this.mMySP.edit();
        }

        this.checkSDCardXMLFile();
    }

    public void putInt(String key, int value) {
        if(!StringUtils.isEmpty(key) && !key.equals("t")) {
            this.initEditor();
            if(this.mEditor != null) {
                this.mEditor.putInt(key, value);
            }

            if(this.mMyEditor != null) {
                this.mMyEditor.putInt(key, value);
            }
        }

    }

    public void putLong(String key, long value) {
        if(!StringUtils.isEmpty(key) && !key.equals("t")) {
            this.initEditor();
            if(this.mEditor != null) {
                this.mEditor.putLong(key, value);
            }

            if(this.mMyEditor != null) {
                this.mMyEditor.putLong(key, value);
            }
        }

    }

    public void putBoolean(String key, boolean value) {
        if(!StringUtils.isEmpty(key) && !key.equals("t")) {
            this.initEditor();
            if(this.mEditor != null) {
                this.mEditor.putBoolean(key, value);
            }

            if(this.mMyEditor != null) {
                this.mMyEditor.putBoolean(key, value);
            }
        }

    }

    public void putFloat(String key, float value) {
        if(!StringUtils.isEmpty(key) && !key.equals("t")) {
            this.initEditor();
            if(this.mEditor != null) {
                this.mEditor.putFloat(key, value);
            }

            if(this.mMyEditor != null) {
                this.mMyEditor.putFloat(key, value);
            }
        }

    }

    public void putString(String key, String value) {
        if(!StringUtils.isEmpty(key) && !key.equals("t")) {
            this.initEditor();
            if(this.mEditor != null) {
                this.mEditor.putString(key, value);
            }

            if(this.mMyEditor != null) {
                this.mMyEditor.putString(key, value);
            }
        }

    }

    public void remove(String key) {
        if(!StringUtils.isEmpty(key) && !key.equals("t")) {
            this.initEditor();
            if(this.mEditor != null) {
                this.mEditor.remove(key);
            }

            if(this.mMyEditor != null) {
                this.mMyEditor.remove(key);
            }
        }

    }

    public void reload() {
        if(this.mSp != null && this.mContext != null) {
            this.mSp = this.mContext.getSharedPreferences(this.mConfigName, 0);
        }

        String mountedProperty = Environment.getExternalStorageState();
        if(!StringUtils.isEmpty(mountedProperty) && (mountedProperty.equals("mounted") || mountedProperty.equals("mounted_ro") && this.mMySP != null)) {
            try {
                if(this.mTxf != null) {
                    this.mMySP = this.mTxf.getMySharedPreferences(this.mConfigName, 0);
                }
            } catch (Exception var3) {
                ;
            }
        }

    }

    public void clear() {
        this.initEditor();
        long t = System.currentTimeMillis();
        if(this.mEditor != null) {
            this.mEditor.clear();
            this.mEditor.putLong("t", t);
        }

        if(this.mMyEditor != null) {
            this.mMyEditor.clear();
            this.mMyEditor.putLong("t", t);
        }

    }

    public boolean commit() {
        boolean result = true;
        long t = System.currentTimeMillis();
        if(this.mEditor != null) {
            if(!this.mIsLessMode && this.mSp != null) {
                this.mEditor.putLong("t", t);
            }

            if(!this.mEditor.commit()) {
                result = false;
            }
        }

        if(this.mSp != null && this.mContext != null) {
            this.mSp = this.mContext.getSharedPreferences(this.mConfigName, 0);
        }

        String mountedProperty = Environment.getExternalStorageState();
        if(!StringUtils.isEmpty(mountedProperty)) {
            if(mountedProperty.equals("mounted")) {
                if(this.mMySP == null) {
                    TransactionXMLFile e = this.getTransactionXMLFile(this.mFolderName);
                    if(e != null) {
                        this.mMySP = e.getMySharedPreferences(this.mConfigName, 0);
                        if(!this.mIsLessMode) {
                            this.copySPToMySP(this.mSp, this.mMySP);
                        } else {
                            this.copyMySPToSP(this.mMySP, this.mSp);
                        }

                        this.mMyEditor = this.mMySP.edit();
                    }
                } else if(this.mMyEditor != null && !this.mMyEditor.commit()) {
                    result = false;
                }
            }

            if(mountedProperty.equals("mounted") || mountedProperty.equals("mounted_ro") && this.mMySP != null) {
                try {
                    if(this.mTxf != null) {
                        this.mMySP = this.mTxf.getMySharedPreferences(this.mConfigName, 0);
                    }
                } catch (Exception var6) {
                    ;
                }
            }
        }

        return result;
    }

    public String getString(String key) {
        this.checkSDCardXMLFile();
        if(this.mSp != null) {
            String value = this.mSp.getString(key, "");
            if(!StringUtils.isEmpty(value)) {
                return value;
            }
        }

        return this.mMySP != null?this.mMySP.getString(key, ""):"";
    }

    public int getInt(String key) {
        this.checkSDCardXMLFile();
        return this.mSp != null?this.mSp.getInt(key, 0):(this.mMySP != null?this.mMySP.getInt(key, 0):0);
    }

    public long getLong(String key) {
        this.checkSDCardXMLFile();
        return this.mSp != null?this.mSp.getLong(key, 0L):(this.mMySP != null?this.mMySP.getLong(key, 0L):0L);
    }

    public float getFloat(String key) {
        this.checkSDCardXMLFile();
        return this.mSp != null?this.mSp.getFloat(key, 0.0F):(this.mMySP != null?this.mMySP.getFloat(key, 0.0F):0.0F);
    }

    public boolean getBoolean(String key) {
        this.checkSDCardXMLFile();
        return this.mSp != null?this.mSp.getBoolean(key, false):(this.mMySP != null?this.mMySP.getBoolean(key, false):false);
    }

    public Map<String, ?> getAll() {
        this.checkSDCardXMLFile();
        return this.mSp != null?this.mSp.getAll():(this.mMySP != null?this.mMySP.getAll():null);
    }
}
