public class AppController {
    private static final String LOG = AppController.class.getSimpleName();

    private static AppController mInstance;

    private AppCompatActivity mMainScreenActivity = null;

    private boolean mIsBackended = false;

    private static Activity mCurrentActivityContext = null;

    private TabContacts mContactsTab;
    private TabChats mChatsTab;

    private DatabaseHelper mDatabaseHelper;
    private TorProcessManager mTorProcessManager;
    private TextView mTorStatusCarrier;

    private boolean isFirstRun = true;

    private long mBundlePid;
    private String mMyAddress;

    private HistoryCleanupManager mHistoryCleanupManager;

    private AppController(AppCompatActivity mainActivity) {
        mMainScreenActivity = mainActivity;
//        binding the textView to the MainScreen
        mTorStatusCarrier = new TextView(mMainScreenActivity.getApplicationContext());

        initBackend();
    }

    /**
     * To be used with non-null Context the first time this object is created in the most early app time
     * In rest to be used with null Context parameter
     */
    public static synchronized AppController getInstanceParameterized(AppCompatActivity mainActivity) {
        if (mInstance == null) {
            mInstance = new AppController(mainActivity);
        }
        return mInstance;
    }

    /**
     * binding the backend functionality to MainScreen context
     */
    private void initBackend() {
        Log.i(LOG, "initBackend -> ENTER");

//        Database init
        mDatabaseHelper = new DatabaseHelper(mMainScreenActivity.getApplicationContext(), PersistenceConstants.DATABASE_ANONYMITY_CHAT,
                null, PersistenceConstants.DATABASE_VERSION);
        mDatabaseHelper.initOperations();
        mDatabaseHelper.triggerInsertDefaultMyProfile();
        Log.i(LOG, "init -> mDatabaseHelper - DONE");

        mHistoryCleanupManager = new HistoryCleanupManager();

        final AppController appController = this;
        mHistoryCleanupManager.init(appController);
        Log.i(LOG, "init -> mHistoryCleanupManager - DONE");

        mBundlePid = mDatabaseHelper.getMyProfileOperations().getBundlePid(1);
        Log.i(LOG, "bundlePidLong=" + mBundlePid);

//        handling the previous instance of the app which left the bundle process open
        if (mBundlePid > 0) {
            Log.i(LOG, "the previous bundle process needs to be cleaned: mBundlePid=" + mBundlePid);

            Log.e(LOG, "Killing the previous TorProcess");
            android.os.Process.killProcess((int) getBundlePid());
            Log.e(LOG, "succeded. Now updating the current PID");

            updateBundlePid(0);
        }

        mTorProcessManager = new TorProcessManager(mMainScreenActivity, mTorStatusCarrier);
//        TODO: activate automatic TOR connection
//        Automatic TOR connection start
//        final String networkStatus = mTorStatusCarrier.getText().toString().trim();
//
//        if (!mTorProcessManager.isTorBundleStarted() && (networkStatus.equals(TorConstants.TOR_BUNDLE_STOPPED))) {
//            mTorProcessManager.startTorBundle();
//
//            detectFirstRun();
//        }

        Log.i(LOG, "init -> mTorProcessManager - DONE");

// initializing the ChatController and implicitly the TorReceiver
        mMyAddress = getMyProfile().getMyAddress();
        handleChatController();
        Log.i(LOG, "init -> ChatController - DONE");

        Log.i(LOG, "initBackend -> LEAVE");
    }

    public void startNetworkConnection() {
        Log.i(LOG, "startNetworkConnection -> ENTER");

        final String networkStatus = mTorStatusCarrier.getText().toString().trim();

        if (!mTorProcessManager.isTorBundleStarted() && (networkStatus.equals(TorConstants.TOR_BUNDLE_STOPPED))) {
            mTorProcessManager.startTorBundle();

            detectFirstRun();
        }

        Log.i(LOG, "startNetworkConnection -> LEAVE");
    }

    public void stopHistoryCleanupJob() {
        Log.i(LOG, "stopHistoryCleanupJob -> ENTER");

        mHistoryCleanupManager.stopAllCleanupJobs();

        Log.i(LOG, "stopHistoryCleanupJob -> LEAVE");
    }

    public void stopNetworkConnection() {
        Log.i(LOG, "stopNetworkConnection -> ENTER");

        mTorProcessManager.stopTorBundle();

        Log.i(LOG, "stopNetworkConnection -> LEAVE");
    }

    private void detectFirstRun() {
        Log.i(LOG, "detectFirstRun -> ENTER");

        mTorStatusCarrier.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.i(LOG, "detectFirstRun.onTextChanged -> LEAVE charSequence=" + charSequence);

                final StringBuilder sb = new StringBuilder(charSequence.length());
                sb.append(charSequence);
                String textViewText = sb.toString();

                if (isFirstRun) {
                    if (textViewText.equals(TorConstants.TOR_BUNDLE_STARTED)) {
                        handleFirstRun();
                        isFirstRun = false;

                        mMyAddress = getMyProfile().getMyAddress();
                    }
                }
                if (textViewText.equals(TorConstants.TOR_BUNDLE_STARTED)) {
//                    handleChatController();
                    ChatController.getInstance().setMyAddress(mMyAddress);
                }

//                if (textViewText.equals(TorConstants.TOR_BUNDLE_STOPPED)) {
//                    Log.i(LOG, "tor bundle has stopped ... clearing Chat Controller resources");
//                    // ChatController managed resources --- what is to be set to default
//
//                    ChatController.cleanUp();
//                }

                Log.i(LOG, "detectFirstRun.onTextChanged -> LEAVE");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        Log.i(LOG, "detectFirstRun -> LEAVE");
    }

    private boolean handleFirstRun() {
        Log.i(LOG, "firstRunCheck -> ENTER");
        boolean retVal = false;

        if (mTorProcessManager.isTorBundleStarted()) {

            String myAddress = mTorProcessManager.getTorAddress().substring(0, 16);

            if (myAddress.length() == AppConstants.ADDRESS_SIZE) {

                mDatabaseHelper.getMyProfileOperations().updateAddressOnFirstNetworkConnection(myAddress);

                ChatController.getInstance().setMyAddress(mMyAddress);

                retVal = true;
            }
        }

        Log.i(LOG, "firstRunCheck -> LEAVE retVal=" + retVal);
        return retVal;
    }

    private void handleChatController() {
        Log.i(LOG, "handleChatController -> ENTER");

        ChatController.getInstance();
        ChatController.getInstance().setMyAddress(mMyAddress);
        ChatController.getInstance().setCurrentActivityContext(mMainScreenActivity);
        ChatController.getInstance().initializeChatConnectionManagement();

        ChatController.getInstance().setCurrentActivityContext(mMainScreenActivity);

        Log.i(LOG, "handleChatController -> LEAVE");
    }

    public void updateBundlePid(long newPid) {
        Log.i(LOG, "updateBundlePid -> ENTER newPid=" + newPid);

        mBundlePid = newPid;

        long currentPid = mDatabaseHelper.getMyProfileOperations().getBundlePid(1);

        if (currentPid != newPid) {
            mDatabaseHelper.getMyProfileOperations().updateBundlePid(newPid);
        }

        Log.i(LOG, "updateBundlePid -> LEAVE");
    }

    public long getBundlePid() {
        Log.i(LOG, "getBundlePid -> ENTER");

        Log.i(LOG, "getBundlePid -> LEAVE mBundlePid=" + mBundlePid);
        return mBundlePid;
    }

    public void setContactsTab(TabContacts contactsTab) {
        Log.i(LOG, "setContactsTab -> ENTER");
        mContactsTab = contactsTab;
        Log.i(LOG, "setContactsTab -> LEAVE");
    }

    public void addNewContactToTab(ContactEntity newContactEntity) {
        Log.i(LOG, "addNewContactToTab -> ENTER");
        mContactsTab.addContactToList(newContactEntity);
        Log.i(LOG, "addNewContactToTab -> LEAVE");
    }

    public void removeContactFromTab(ContactEntity newContactEntity) {
        Log.i(LOG, "removeContactFromTab -> ENTER");
        mContactsTab.removeContactFromList(newContactEntity);
        Log.i(LOG, "removeContactFromTab -> LEAVE");
    }

    public void setChatsTab(TabChats chatsTab) {
        Log.i(LOG, "setChatsTab -> ENTER");
        mChatsTab = chatsTab;
        Log.i(LOG, "setChatsTab -> LEAVE");
    }

//    private void addNewChatToTab(ChatEntity newChatEntity) {
//        Log.i(LOG, "addNewChatToTab -> ENTER newChatEntity=" + newChatEntity);
//        mChatsTab.addChatToList(newChatEntity);
//        Log.i(LOG, "addNewChatToTab -> LEAVE");
//    }

    public void editChatToTab(ChatEntity chatEntity) {
        Log.i(LOG, "editChatToTab -> ENTER chatEntity=" + chatEntity);
        mChatsTab.updateContactList(chatEntity);
        Log.i(LOG, "editChatToTab -> LEAVE");
    }

    public void editContactToTab(ContactEntity contactEntity) {
        Log.i(LOG, "editContactToTab -> ENTER contactEntity=" + contactEntity);
        mContactsTab.updateContactList(contactEntity);
        Log.i(LOG, "editContactToTab -> LEAVE");
    }

    public void addMessageToChatHistory(DBChatMessageEntity chatMessageEntity) {
        Log.i(LOG, "addMessageToChatHistory -> ENTER chatMessageEntity=" + chatMessageEntity);
        mDatabaseHelper.getChatMessagesOperations().addDbEntity(chatMessageEntity);
        Log.i(LOG, "addMessageToChatHistory -> LEAVE");
    }

    public List<IDbEntity> getMessageHistoryForSessionId(long contactSessionId) {
        Log.i(LOG, "getMessageHistory -> ENTER contactSessionId=" + contactSessionId);
        List<IDbEntity> retVal;

        retVal = mDatabaseHelper.getChatMessagesOperations().getAllMessagesForSessionId(contactSessionId);

        Log.i(LOG, "getMessageHistory -> LEAVE retVal=" + retVal + ", retVal.size()=" + retVal.size());
        return retVal;
    }

    public void removeMessagesForChat(long contactSessionId) {
        Log.i(LOG, "removeMessagesForChat -> ENTER contactSessionId=" + contactSessionId);
        mDatabaseHelper.getChatMessagesOperations().deleteAllMessagesForSessionId(contactSessionId);
        Log.i(LOG, "removeMessagesForChat -> LEAVE");
    }

//    public void moveToChatsTab() {
//        Log.i(LOG, "moveToChatsTab -> ENTER");
//        ((MainScreenActivity) mMainScreenActivity).moveToChatsTab();
//        Log.i(LOG, "moveToChatsTab -> LEAVE");
//    }

    public void setChatControllerCurrentActivityContext(Activity context) {
        Log.i(LOG, "setChatControllerCurrentActivityContext -> ENTER context=" + context);
        ChatController.getInstance().setCurrentActivityContext(context);
        mCurrentActivityContext = context;
        Log.i(LOG, "setChatControllerCurrentActivityContext -> LEAVE");
    }

    public Activity getCurrentActivityContext() {
        return mCurrentActivityContext;
    }

    public void updateWithNetworkConnectionStatus(final TextView networkConnStatusLabel) {
        Log.i(LOG, "getNetworkConnectionStatus -> ENTER networkConnStatusLabel=" + networkConnStatusLabel);

        mTorStatusCarrier.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                networkConnStatusLabel.setText(mTorStatusCarrier.getText());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        Log.i(LOG, "getNetworkConnectionStatus -> LEAVE");
    }

    public String getNetworkConnectionStatus() {
        Log.i(LOG, "getNetworkConnectionStatus -> ENTER");

        String retVal = mTorStatusCarrier.getText().toString();

        Log.i(LOG, "getNetworkConnectionStatus -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public void triggerNotification(String partnerAddress) {
        Log.i(LOG, "triggerNotification -> ENTER");

        if (mIsBackended) {
            IDbEntity iDbEntityByAddress = mDatabaseHelper.getContactsOperations().getIDbEntityByAddress(partnerAddress);

            String title = "";

            if (iDbEntityByAddress != null) {
                DBContactEntity dbContactEntity = (DBContactEntity) iDbEntityByAddress;
                title = dbContactEntity.getName() + " contacted you!";
            } else {
                title = partnerAddress.substring(0, 16) + " contacted you!";
            }

            ((MainScreenActivity) mMainScreenActivity).triggerNotification(title, "Incoming chat request from your contacts!");

        }

        Log.i(LOG, "triggerNotification -> LEAVE");
    }

    public void setCurrentActivityContext(Activity context) {
        Log.i(LOG, "setCurrentActivityContext -> ENTER context=" + context);

        mCurrentActivityContext = context;

        Log.i(LOG, "setCurrentActivityContext -> LEAVE");
    }

    //    TODO: all DB interaction
    public void openDB(SQLiteDatabase sqLiteDatabase) {
        Log.i(LOG, "openDB -> ENTER");

        mDatabaseHelper.onOpen(sqLiteDatabase);

        Log.i(LOG, "openDB -> LEAVE");
    }

    public void closeDB() {
        Log.i(LOG, "closeDB -> ENTER");

        mDatabaseHelper.close();

        Log.i(LOG, "closeDB -> LEAVE");
    }

    public DBMyProfileEntity getMyProfile() {
        Log.i(LOG, "getMyProfile -> ENTER");
        DBMyProfileEntity retVal = null;

//        retVal = mDatabaseHelper.getMyProfileOperations().getAllIDbEntity().isEmpty() ?
//                null : (DBMyProfileEntity) mDatabaseHelper.getMyProfileOperations().getAllIDbEntity().get(0);

        retVal = (DBMyProfileEntity) mDatabaseHelper.getMyProfileOperations().getAllIDbEntity().get(0);

        Log.i(LOG, "getMyProfile -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public void updateMyProfile(DBMyProfileEntity myProfileEntity) {
        Log.i(LOG, "updateMyProfile -> ENTER");
        mDatabaseHelper.getMyProfileOperations().updateDbEntity(myProfileEntity);
        Log.i(LOG, "updateMyProfile -> LEAVE");
    }

    public boolean addNewContact(DBContactEntity contactEntity) {
        Log.i(LOG, "addNewContact -> ENTER contactEntity=" + contactEntity);
        boolean retVal;

        final DBContactEntity contact = (DBContactEntity) mDatabaseHelper.getContactsOperations().getIDbEntityByAddress(contactEntity.getAddress());

        if (contact == null) {
            contactEntity.setSessionId(generateSessionId());
            mDatabaseHelper.getContactsOperations().addDbEntity(contactEntity);

            addNewChatForContact(contactEntity);

            retVal = true;

        } else {
            retVal = false;
        }

        Log.i(LOG, "addNewContact -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public boolean updateContact(DBContactEntity contactEntity) {
        Log.i(LOG, "addNewContact -> ENTER contactEntity=" + contactEntity);
        boolean retVal;

//        final DBContactEntity contact = (DBContactEntity) mDatabaseHelper.getContactsOperations().getIDbEntityByAddress(contactEntity.getAddress());

        final int updateSuccesfull = mDatabaseHelper.getContactsOperations().updateDbEntity(contactEntity);

        if (updateSuccesfull > 0) {
            retVal = true;
        } else {
            retVal = false;
        }

        Log.i(LOG, "addNewContact -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public boolean deleteContact(String contactAddress) {
        Log.i(LOG, "deleteContact -> ENTER contactAddress=" + contactAddress);
        boolean retVal = false;

        final DBContactEntity contact = (DBContactEntity) mDatabaseHelper.getContactsOperations().getIDbEntityByAddress(contactAddress);

        // if the contact to be updated is found
        if (contact != null) {
            mDatabaseHelper.getChatMessagesOperations().deleteAllMessagesForSessionId(contact.getSessionId());
            Log.i(LOG, "all messages deleted");

            DBChatEntity chatEntity = new DBChatEntity();
            chatEntity.setSessionId(contact.getSessionId());
            boolean chatDeleted = mDatabaseHelper.getChatsOperations().deleteDbEntity(chatEntity);

            if (chatDeleted) {
                Log.i(LOG, "chat deleted -> ");

                retVal = mDatabaseHelper.getContactsOperations().deleteDbEntity(contact);
                Log.i(LOG, "contact deleted -> ");

                mHistoryCleanupManager.removeHistoryCleanupJob(chatEntity.getSessionId());
                Log.i(LOG, "history cleanup job deleted -> ");

                ContactEntity contactEntity = new ContactEntity(contact.getName(), contact.getSessionId());
                removeContactFromTab(contactEntity);
            }
        }

        Log.i(LOG, "deleteContact -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public DBContactEntity getContactEntity(long sessionId) {
        Log.i(LOG, "getContactEntity -> ENTER sessionId=" + sessionId);
        DBContactEntity retVal = null;

        retVal = (DBContactEntity) mDatabaseHelper.getContactsOperations().getIDbEntityById(sessionId);

        Log.i(LOG, "getContactEntity -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public DBContactEntity getContactEntity(String address) {
        Log.i(LOG, "getContactEntity -> ENTER address=" + address);
        DBContactEntity retVal = null;

        retVal = (DBContactEntity) mDatabaseHelper.getContactsOperations().getIDbEntityByAddress(address);

        Log.i(LOG, "getContactEntity -> LEAVE retVal=" + retVal);
        return retVal;
    }

    private boolean addNewChatForContact(DBContactEntity contactEntity) {
        Log.i(LOG, "addNewChatForContact -> ENTER contactEntity=" + contactEntity);
        boolean retVal;

        DBChatEntity chatEntity = new DBChatEntity();
        chatEntity.setSessionId(contactEntity.getSessionId());
        chatEntity.setChatName(contactEntity.getAddress());
        chatEntity.setHistoryCleanupTime(0);

        retVal = mDatabaseHelper.getChatsOperations().addDbEntity(chatEntity);

        mHistoryCleanupManager.addHistoryCleanupJob(chatEntity.getSessionId(), chatEntity.getHistoryCleanupTime());


//        if (retVal) {
////            TODO: fix chat availability
//            ChatEntity tabChatEntity = new ChatEntity(chatEntity.getSessionId(), chatEntity.getChatName(), false);
//            addNewChatToTab(tabChatEntity);
//        }

        Log.i(LOG, "addNewChatForContact -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public DBChatEntity getChatEntity(long sessionId) {
        Log.i(LOG, "getChatEntity -> ENTER sessionId=" + sessionId);
        DBChatEntity retVal = null;

        retVal = (DBChatEntity) mDatabaseHelper.getChatsOperations().getIDbEntityById(sessionId);

        Log.i(LOG, "getChatEntity -> LEAVE retVal=" + retVal);
        return retVal;
    }

//    public DBChatEntity getChatEntity(String address) {
//        Log.i(LOG, "getChatEntity -> ENTER address=" + address);
//        DBChatEntity retVal = null;
//
//        retVal = (DBChatEntity) mDatabaseHelper.getChatsOperations().getIDbEntityById(sessionId);
//
//        Log.i(LOG, "getChatEntity -> LEAVE retVal=" + retVal);
//        return retVal;
//    }

    public boolean updateChat(DBChatEntity chatEntity) {
        Log.i(LOG, "updateChat -> ENTER chatEntity=" + chatEntity);
        boolean retVal = false;

        if (chatEntity != null) {

            mDatabaseHelper.getChatsOperations().updateDbEntity(chatEntity);

            mHistoryCleanupManager.updateHistoryCleanupJob(chatEntity.getSessionId(), chatEntity.getHistoryCleanupTime());

            retVal = true;
        }

        Log.i(LOG, "updateChat -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public List<IDbEntity> getChatList() {
        Log.i(LOG, "getChatList -> ENTER");
        List<IDbEntity> retVal = new ArrayList();

        retVal = mDatabaseHelper.getChatsOperations().getAllIDbEntity();

        Log.i(LOG, "getChatList -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public List<IDbEntity> getContactList() {
        Log.i(LOG, "getContactList -> ENTER");
        List<IDbEntity> retVal = new ArrayList();

        retVal = mDatabaseHelper.getContactsOperations().getAllIDbEntity();

        Log.i(LOG, "getContactList -> LEAVE retVal=" + retVal);
        return retVal;
    }

    private long generateSessionId() {
        Log.i(LOG, "generateSessionId -> ENTER");

        long retVal = UUID.randomUUID().getMostSignificantBits();

        Log.i(LOG, "generateSessionId -> LEAVE retVal=" + retVal);
        return retVal;
    }

    public void setIsBackended(boolean isBackended) {
        Log.i(LOG, "setIsBackended -> ENTER isBackended=" + isBackended);
        mIsBackended = isBackended;
        Log.i(LOG, "setIsBackended -> LEAVE");
    }

//    TODO create fontAwesome loading method here
//    a generic FA loading method used throughout the app
} public class NavigationDrawerDivider extends NavigationDrawerEntry {
} public class NavigationDrawerEntry {
} public class NavigationDrawerItem extends NavigationDrawerEntry {
    private String title;

    public NavigationDrawerItem(String title) {
        this.setTitle(title);
    }

    public String getTitle() {
        return title;
    }

    private void setTitle(String title) {
        this.title = title;
    }
} public class NavigationDrawerItemAndImg extends NavigationDrawerEntry {
    private String title;
    private int iconId;

    public NavigationDrawerItemAndImg(String title, int iconId) {
        this.setTitle(title);
        this.setIconId(iconId);
    }

    public int getIconId() {
        return iconId;
    }

    private void setIconId(int iconId) {
        this.iconId = iconId;
    }

    public String getTitle() {
        return title;
    }

    private void setTitle(String title) {
        this.title = title;
    }
} public class NavigationDrawerToogle extends NavigationDrawerEntry {
    private String title;
    private boolean checked;

    public NavigationDrawerToogle(String title) {
        this.setTitle(title);
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public String getTitle() {
        return title;
    }

    private void setTitle(String title) {
        this.title = title;
    }
} public class SettingsAboutActivity extends AppCompatActivity {
    private static final String LOG = SettingsAboutActivity.class.getSimpleName();

    private Fragment mThis = null;

    //    private View mView;
    private TextView mBackIcon;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(LOG, "onCreate -> ENTER");
        super.onCreate(savedInstanceState);

//        mThis = this;

        setContentView(R.layout.settings_about_activity);

        AppController.getInstanceParameterized(null).setChatControllerCurrentActivityContext(this);

        //Loading fontAwesome
        Typeface fontAwesome = Typeface.createFromAsset(getAssets(),
                "font_awesome/fontawesome.ttf");

////        Hiding the floating Add contact button
//        TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//        if (contactsFragment != null) {
//            contactsFragment.getView().setVisibility(View.INVISIBLE);
//        }

        mBackIcon = (TextView) findViewById(R.id.about_back_icon);
        mBackIcon.setTypeface(fontAwesome);

        mBackIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(LOG, "mBackIcon.onClick -> ENTER");
//                getActivity().getFragmentManager().beginTransaction().remove(mThis).commit();
//
//                TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//                if (contactsFragment != null) {
//                    contactsFragment.getView().setVisibility(View.VISIBLE);
//                }

                finish();
                Log.i(LOG, "mBackIcon.onClick -> LEAVE");
            }

        });

        Log.i(LOG, "onCreate -> LEAVE");
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//        Log.i(LOG, "onCreateView -> ENTER");
//        mView = inflater.inflate(R.layout.settings_about_activity, container, false);
//
//        //Loading fontAwesome
//        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
//                "font_awesome/fontawesome.ttf");
//
////        Hiding the floating Add contact button
//        TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//        if (contactsFragment != null) {
//            contactsFragment.getView().setVisibility(View.INVISIBLE);
//        }
//
//        mBackIcon = (TextView) mView.findViewById(R.id.about_back_icon);
//        mBackIcon.setTypeface(fontAwesome);
//
//        mBackIcon.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().getFragmentManager().beginTransaction().remove(mThis).commit();
//
//                TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//                if (contactsFragment != null) {
//                    contactsFragment.getView().setVisibility(View.VISIBLE);
//                }
//            }
//        });
//
//        Log.i(LOG, "onCreateView -> LEAVE");
//        return mView;
//    }

    /**
     * Creating Intent for the activity of this class
     *
     * @param context
     * @return Intent
     */
    public static Intent makeIntent(Context context) {
        Log.i(LOG, "makeIntent -> ENTER context=" + context);

        Intent retVal = new Intent(context, SettingsAboutActivity.class);

        Log.i(LOG, "makeIntent -> LEAVE retVal=" + retVal);
        return retVal;
    }
} public class SettingsNetworkActivity extends AppCompatActivity {
    private static final String LOG = SettingsNetworkActivity.class.getSimpleName();

//    private Fragment mThis = null;

//    private View mView;
    private TextView mBackIcon;
    private Switch mNetworkConnectionSwitch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(LOG, "onCreate -> ENTER");
        super.onCreate(savedInstanceState);

//        mThis = this;

        setContentView(R.layout.settings_network_activity);

        AppController.getInstanceParameterized(null).setChatControllerCurrentActivityContext(this);

        //Loading fontAwesome
        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
                "font_awesome/fontawesome.ttf");

        mBackIcon = (TextView) findViewById(R.id.settings_network_back);
        mBackIcon.setTypeface(fontAwesome);

        mBackIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mNetworkConnectionSwitch = (Switch) findViewById(R.id.settings_network_connection_toogle);
        mNetworkConnectionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Log.i(LOG, "mNetworkConnectionSwitch.onCheckedChanged -> ENTER");

                if (isChecked) {
                    AppController.getInstanceParameterized(null).startNetworkConnection();
                } else {
                    AppController.getInstanceParameterized(null).stopNetworkConnection();
                }

                Log.i(LOG, "mNetworkConnectionSwitch.onCheckedChanged -> LEAVE");
            }
        });

        if (AppController.getInstanceParameterized(null).getNetworkConnectionStatus().equals(TorConstants.TOR_BUNDLE_STOPPED)) {
            mNetworkConnectionSwitch.setChecked(false);
        } else {
            mNetworkConnectionSwitch.setChecked(true);
        }

        Log.i(LOG, "onCreate -> LEAVE");
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//        Log.i(LOG, "onCreateView -> ENTER");
//        mView = inflater.inflate(R.layout.settings_network_activity, container, false);
//
//        //Loading fontAwesome
//        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
//                "font_awesome/fontawesome.ttf");
//
////        Hiding the floating Add contact button
//        TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//        if (contactsFragment != null) {
//            contactsFragment.getView().setVisibility(View.INVISIBLE);
//        }
//
//        mBackIcon = (TextView) mView.findViewById(R.id.settings_network_back);
//        mBackIcon.setTypeface(fontAwesome);
//
//        mBackIcon.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().getFragmentManager().beginTransaction().remove(mThis).commit();
//
//                TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//                if (contactsFragment != null) {
//                    contactsFragment.getView().setVisibility(View.VISIBLE);
//                }
//            }
//        });
//
//        mNetworkConnectionSwitch = (Switch) mView.findViewById(R.id.settings_network_connection_toogle);
//        mNetworkConnectionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//                Log.i(LOG, "mNetworkConnectionSwitch.onCheckedChanged -> ENTER");
//
//                if (isChecked) {
//                    AppController.getInstanceParameterized(null).startNetworkConnection();
//                } else {
//                    AppController.getInstanceParameterized(null).stopNetworkConnection();
//                }
//
//                Log.i(LOG, "mNetworkConnectionSwitch.onCheckedChanged -> LEAVE");
//            }
//        });
//
//        if (AppController.getInstanceParameterized(null).getNetworkConnectionStatus().equals(TorConstants.TOR_BUNDLE_STOPPED)) {
//            mNetworkConnectionSwitch.setChecked(false);
//        } else {
//            mNetworkConnectionSwitch.setChecked(true);
//        }
//
//        Log.i(LOG, "onCreateView -> LEAVE");
//        return mView;
//    }

//    @Override
//    public void onStart() {
//        Log.i(LOG, "onStart -> ENTER");
//        super.onStart();
//        Log.i(LOG, "onStart -> LEAVE");
//    }

    /**
     * Creating Intent for the activity of this class
     *
     * @param context
     * @return Intent
     */
    public static Intent makeIntent(Context context) {
        Log.i(LOG, "makeIntent -> ENTER context=" + context);

        Intent retVal = new Intent(context, SettingsNetworkActivity.class);

        Log.i(LOG, "makeIntent -> LEAVE retVal=" + retVal);
        return retVal;
    }

//    @Override
//    public void onDestroyView() {
//        Log.i(LOG, "onDestroyView -> ENTER");
//        super.onDestroyView();
//        Log.i(LOG, "onDestroyView -> LEAVE");
//    }
} public class SettingsProfileActivity extends AppCompatActivity {
    private static final String LOG = SettingsProfileActivity.class.getSimpleName();

    private TextView mBackIcon, mMyAddress, mSave;
    private EditText mMyName;
    //    private EditText mMyNickname;
    private DBMyProfileEntity mMyProfileEntity;


//    private Fragment mThis = null;
//
//    private View mView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(LOG, "onCreate -> ENTER");
        super.onCreate(savedInstanceState);

//        mThis = this;

        setContentView(R.layout.settings_profile_activity);

        AppController.getInstanceParameterized(null).setChatControllerCurrentActivityContext(this);

        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
                "font_awesome/fontawesome.ttf");

        // load my profile
        mMyProfileEntity = AppController.getInstanceParameterized(null).getMyProfile();

        mBackIcon = (TextView) findViewById(R.id.settings_profile_back);
        mBackIcon.setTypeface(fontAwesome);

        mBackIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mMyName = (EditText) findViewById(R.id.my_name_edit);
        mMyName.setText(mMyProfileEntity.getMyName());
//        mMyNickname = (EditText) mView.findViewById(R.id.my_nickname_edit);
//        mMyNickname.setText(mMyProfileEntity.getMyNickName());

        mMyAddress = (TextView) findViewById(R.id.my_address_edit);
//        Without the suffix
        mMyAddress.setText(mMyProfileEntity.getMyAddress().substring(0, 16));

//        mMyEmail = (EditText) findViewById(R.id.email_contact_edit);
//        mMyEmail.setText(mMyProfileEntity.getMyEmail());

        mSave = (TextView) findViewById(R.id.settings_my_profile_save);

        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String myName = getTextFromEditText(mMyName);
                String myNickName = myName;
//                String myNickName = getTextFromEditText(mMyNickname);
//                String myEmail = getTextFromEditText(mMyEmail);

//                if (!myName.equals(mMyProfileEntity.getMyName().toString()) ||
//                        !myNickName.equals(mMyProfileEntity.getMyNickName().toString()) ||
//                        !myEmail.equals(mMyProfileEntity.getMyEmail().toString())) {

                if (!myName.equals(mMyProfileEntity.getMyName().toString()) ||
                        !myNickName.equals(mMyProfileEntity.getMyNickName().toString())) {

                    mMyProfileEntity.setMyName(myName);
                    mMyProfileEntity.setMyNickName(myNickName);
//                    mMyProfileEntity.setMyEmail(myEmail);
                    mMyProfileEntity.setMyEmail(null);

                    AppController.getInstanceParameterized(null).updateMyProfile(mMyProfileEntity);

                    finish();
                }
            }
        });

        Log.i(LOG, "onCreate -> LEAVE");
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//        Log.i(LOG, "onCreateView -> ENTER");
//        mView = inflater.inflate(R.layout.settings_profile_activity, container, false);
//
//        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
//                "font_awesome/fontawesome.ttf");
//
////        Hiding the floating Add contact button
//        TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//        if (contactsFragment != null) {
//            contactsFragment.getView().setVisibility(View.INVISIBLE);
//        }
//        // load my profile
//        mMyProfileEntity = AppController.getInstanceParameterized(null).getMyProfile();
//
//        mBackIcon = (TextView) mView.findViewById(R.id.settings_profile_back);
//        mBackIcon.setTypeface(fontAwesome);
//
//        mBackIcon.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().getFragmentManager().beginTransaction().remove(mThis).commit();
//
//                TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
//                if (contactsFragment != null) {
//                    contactsFragment.getView().setVisibility(View.VISIBLE);
//                }
//            }
//        });
//
//        mMyName = (EditText) mView.findViewById(R.id.my_name_edit);
//        mMyName.setText(mMyProfileEntity.getMyName());
////        mMyNickname = (EditText) mView.findViewById(R.id.my_nickname_edit);
////        mMyNickname.setText(mMyProfileEntity.getMyNickName());
//
//        mMyAddress = (TextView) mView.findViewById(R.id.my_address_edit);
////        Without the suffix
//        mMyAddress.setText(mMyProfileEntity.getMyAddress().substring(0, 16));
//
////        mMyEmail = (EditText) findViewById(R.id.email_contact_edit);
////        mMyEmail.setText(mMyProfileEntity.getMyEmail());
//
//        mSave = (TextView) mView.findViewById(R.id.settings_my_profile_save);
//
//        mSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String myName = getTextFromEditText(mMyName);
//                String myNickName = myName;
////                String myNickName = getTextFromEditText(mMyNickname);
////                String myEmail = getTextFromEditText(mMyEmail);
//
////                if (!myName.equals(mMyProfileEntity.getMyName().toString()) ||
////                        !myNickName.equals(mMyProfileEntity.getMyNickName().toString()) ||
////                        !myEmail.equals(mMyProfileEntity.getMyEmail().toString())) {
//
//                if (!myName.equals(mMyProfileEntity.getMyName().toString()) ||
//                        !myNickName.equals(mMyProfileEntity.getMyNickName().toString())) {
//
//                    mMyProfileEntity.setMyName(myName);
//                    mMyProfileEntity.setMyNickName(myNickName);
////                    mMyProfileEntity.setMyEmail(myEmail);
//                    mMyProfileEntity.setMyEmail(null);
//
//                    AppController.getInstanceParameterized(null).updateMyProfile(mMyProfileEntity);
//                }
//            }
//        });
//
//        Log.i(LOG, "onCreateView -> LEAVE");
//        return mView;
//    }

//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        Log.i(LOG, "onCreate -> ENTER");
//
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.settings_profile_activity);
//
//        AppController.getInstanceParameterized(null).setChatControllerCurrentActivityContext(this);
//
//        Typeface fontAwesome = Typeface.createFromAsset(getAssets(), "font_awesome/fontawesome.ttf");
//
//        // load my profile
//        mMyProfileEntity = AppController.getInstanceParameterized(null).getMyProfile();
//
////        mEmailIcon = (TextView) findViewById(R.id.contact_email_icon_edit);
////        mEmailIcon.setTypeface(fontAwesome);
//
//        mBackIcon = (TextView) findViewById(R.id.settings_profile_back);
//        mBackIcon.setTypeface(fontAwesome);
//        mBackIcon.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Log.i(LOG, "mBackIcon.setOnClickListener -> ENTER");
//                finish();
//                Log.i(LOG, "mBackIcon.setOnClickListener -> LEAVE");
//            }
//        });
//
//        mMyName = (EditText) findViewById(R.id.my_name_edit);
//        mMyName.setText(mMyProfileEntity.getMyName());
//        mMyNickname = (EditText) findViewById(R.id.my_nickname_edit);
//        mMyNickname.setText(mMyProfileEntity.getMyNickName());
//
//        mMyAddress = (TextView) findViewById(R.id.my_address_edit);
////        Without the suffix
//        mMyAddress.setText(mMyProfileEntity.getMyAddress().substring(0, 16));
//
////        mMyEmail = (EditText) findViewById(R.id.email_contact_edit);
////        mMyEmail.setText(mMyProfileEntity.getMyEmail());
//
//        mSave = (TextView) findViewById(R.id.settings_my_profile_save);
//
//        mSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String myName = getTextFromEditText(mMyName);
//                String myNickName = getTextFromEditText(mMyNickname);
////                String myEmail = getTextFromEditText(mMyEmail);
//
////                if (!myName.equals(mMyProfileEntity.getMyName().toString()) ||
////                        !myNickName.equals(mMyProfileEntity.getMyNickName().toString()) ||
////                        !myEmail.equals(mMyProfileEntity.getMyEmail().toString())) {
//
//                if (!myName.equals(mMyProfileEntity.getMyName().toString()) ||
//                        !myNickName.equals(mMyProfileEntity.getMyNickName().toString())) {
//
//                    mMyProfileEntity.setMyName(myName);
//                    mMyProfileEntity.setMyNickName(myNickName);
////                    mMyProfileEntity.setMyEmail(myEmail);
//                    mMyProfileEntity.setMyEmail(null);
//
//                    AppController.getInstanceParameterized(null).updateMyProfile(mMyProfileEntity);
//                }
//            }
//        });
//
//        Log.i(LOG, "onCreate -> ENTER");
//    }

    private String getTextFromEditText(EditText editText) {
        Log.i(LOG, "getTextFromEditText -> ENTER editText=" + editText);
        String retVal = "";

        final StringBuilder sb = new StringBuilder(editText.getText().length());
        sb.append(editText.getText());
        retVal = sb.toString();

        Log.i(LOG, "getTextFromEditText -> LEAVE retVal=" + retVal);
        return retVal;
    }

    /**
     * Creating Intent for the activity of this class
     *
     * @param context
     * @return Intent
     */
    public static Intent makeIntent(Context context) {
        Log.i(LOG, "makeIntent -> ENTER context=" + context);

        Intent retVal = new Intent(context, SettingsProfileActivity.class);

        Log.i(LOG, "makeIntent -> LEAVE retVal=" + retVal);
        return retVal;
    }
} public class SettingsStorageActivity extends AppCompatActivity {
    private static final String LOG = SettingsStorageActivity.class.getSimpleName();

//    private Fragment mThis = null;

    //    private View mView;
    private TextView mBackIcon, mDbSize;
    //    private TextView mClockIcon;
    private Button mDeleteEverything;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(LOG, "onCreate -> ENTER");
        super.onCreate(savedInstanceState);

//        mThis = this;

        setContentView(R.layout.settings_storage_activity);

        AppController.getInstanceParameterized(null).setChatControllerCurrentActivityContext(this);

        //Loading fontAwesome
        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
                "font_awesome/fontawesome.ttf");

        mBackIcon = (TextView) findViewById(R.id.settings_storage_back);
        mBackIcon.setTypeface(fontAwesome);

        mBackIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mDbSize = (TextView) findViewById(R.id.storage_settings_db_size);

        File f = AppController.getInstanceParameterized(null).getCurrentActivityContext().getDatabasePath(PersistenceConstants.DATABASE_ANONYMITY_CHAT);
        long dbSize = f.length();
        Log.i(LOG, "mDbSize -> " + dbSize);

        mDbSize.setText(String.valueOf(dbSize / 1024) + " KB");

        mDeleteEverything = (Button) findViewById(R.id.settings_storage_clear_all);
        mDeleteEverything.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(LOG, "mDeleteEverything.onClick -> ENTER");

                List<IDbEntity> contactList = AppController.getInstanceParameterized(null).getContactList();

                for (IDbEntity dbEntity : contactList) {
                    DBContactEntity dbContactEntity = (DBContactEntity) dbEntity;

                    AppController.getInstanceParameterized(null).deleteContact(dbContactEntity.getAddress());

                }

                Log.i(LOG, "mDeleteEverything.onClick -> LEAVE");
            }
        });

        Log.i(LOG, "onCreate -> LEAVE");
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//        Log.i(LOG, "onCreateView -> ENTER");
//        mView = inflater.inflate(R.layout.settings_storage_activity, container, false);
//
//        //Loading fontAwesome
//        Typeface fontAwesome = Typeface.createFromAsset(AppController.getInstanceParameterized(null).getCurrentActivityContext().getAssets(),
//                "font_awesome/fontawesome.ttf");
//
//////        Hiding the floating Add contact button
////        TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
////        if (contactsFragment != null) {
////            contactsFragment.getView().setVisibility(View.INVISIBLE);
////        }
//
////        mClockIcon = (TextView) mView.findViewById(R.id.settings_storage_clock_icon);
////        mClockIcon.setTypeface(fontAwesome);
//
//        mBackIcon = (TextView) mView.findViewById(R.id.settings_storage_back);
//        mBackIcon.setTypeface(fontAwesome);
//
//        mBackIcon.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().getFragmentManager().beginTransaction().remove(mThis).commit();
//
////                TabContacts contactsFragment = ((MainScreenActivity) getActivity()).getContactsFragment();
////                if (contactsFragment != null) {
////                    contactsFragment.getView().setVisibility(View.VISIBLE);
////                }
//            }
//        });
//
//        mDbSize = (TextView) mView.findViewById(R.id.storage_settings_db_size);
//
//        File f = mView.getContext().getDatabasePath(PersistenceConstants.DATABASE_ANONYMITY_CHAT);
//        long dbSize = f.length();
//        Log.i(LOG, "mDbSize -> " + dbSize);
//
//        mDbSize.setText(String.valueOf(dbSize / 1024) + " KB");
//
//        mDeleteEverything = (Button) mView.findViewById(R.id.settings_storage_clear_all);
//        mDeleteEverything.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Log.i(LOG, "mDeleteEverything.onClick -> ENTER");
//
//                List<IDbEntity> contactList = AppController.getInstanceParameterized(null).getContactList();
//
//                for (IDbEntity dbEntity : contactList) {
//                    DBContactEntity dbContactEntity = (DBContactEntity) dbEntity;
//
//                    AppController.getInstanceParameterized(null).deleteContact(dbContactEntity.getAddress());
//                }
//
//                Log.i(LOG, "mDeleteEverything.onClick -> LEAVE");
//            }
//        });
//
//
//        Log.i(LOG, "onCreateView -> LEAVE");
//        return mView;
//    }

    /**
     * Creating Intent for the activity of this class
     *
     * @param context
     * @return Intent
     */
    public static Intent makeIntent(Context context) {
        Log.i(LOG, "makeIntent -> ENTER context=" + context);

        Intent retVal = new Intent(context, SettingsStorageActivity.class);

        Log.i(LOG, "makeIntent -> LEAVE retVal=" + retVal);
        return retVal;
    }
} public interface MainScreenConstants {
    public static final String CONTACTS_ACTIVITY_INTENT_EXTRA_KEY = "CONTACT_ENTITY";
    public static final String CHATSS_ACTIVITY_INTENT_EXTRA_KEY = "CHAT_ENTITY";

//    Navigator drawer menu

    public static final String NAVIGATION_PROFILE = "My Profile";
    public static final String NAVIGATION_NETWORK = "Network";
    public static final String NAVIGATION_STORAGE = "Storage";
    public static final String NAVIGATION_ABOUT = "About";
    public static final String NAVIGATION_TOOGLE = "Network connection";
}
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import com.batsw.anonimitychat.R;
import com.batsw.anonimitychat.appManagement.AppController;
import com.batsw.anonimitychat.mainScreen.navigation.drawer.entry.NavigationDrawerDivider;
import com.batsw.anonimitychat.mainScreen.navigation.drawer.entry.NavigationDrawerEntry;
import com.batsw.anonimitychat.mainScreen.navigation.drawer.entry.NavigationDrawerItem;
import com.batsw.anonimitychat.mainScreen.navigation.drawer.entry.NavigationDrawerItemAndImg;
import com.batsw.anonimitychat.mainScreen.navigation.drawer.entry.NavigationDrawerToogle;
import com.batsw.anonimitychat.mainScreen.settings.activities.SettingsAboutActivity;
import com.batsw.anonimitychat.mainScreen.settings.activities.SettingsNetworkActivity;
import com.batsw.anonimitychat.mainScreen.settings.activities.SettingsProfileActivity;
import com.batsw.anonimitychat.mainScreen.settings.activities.SettingsStorageActivity;
import com.batsw.anonimitychat.mainScreen.util.MainScreenConstants;
import java.util.List;
package com.batsw.anonimitychat.mainScreen.navigation.drawer;




/**
 * Created by tudor on 4/20/2017.
 */

public class NavigationDrawerMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String LOG = NavigationDrawerMenuAdapter.class.getSimpleName();

    private Context mContext;

    private List<NavigationDrawerEntry> data;
    private LayoutInflater inflater;


    public NavigationDrawerMenuAdapter(Context context, List<NavigationDrawerEntry> data) {
        this.data = data;
        this.inflater = LayoutInflater.from(context);

        mContext = context;

//        AppController.getInstanceParameterized(null).setChatControllerCurrentActivityContext(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View itemLayoutView;
        switch (viewType) {
            case 0:
                itemLayoutView = inflater.inflate(R.layout.main_screen_navigation_drawer_item_and_img, viewGroup, false);
                ItemAndImgViewHolder holder = new ItemAndImgViewHolder(itemLayoutView);
                return holder;
            case 1:
                itemLayoutView = inflater.inflate(R.layout.main_screen_navigation_drawer_divider, viewGroup, false);
                DividerViewHolder dividerViewHolder = new DividerViewHolder(itemLayoutView);
                return dividerViewHolder;
            case 2:
                itemLayoutView = inflater.inflate(R.layout.main_screen_navigation_drawer_item, viewGroup, false);
                ItemViewHolder itemViewHolder = new ItemViewHolder(itemLayoutView);
                return itemViewHolder;
            case 3:
                itemLayoutView = inflater.inflate(R.layout.main_screen_navigation_drawer_toogle, viewGroup, false);
                ToggleViewHolder toggleViewHolder = new ToggleViewHolder(itemLayoutView);
                return toggleViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final NavigationDrawerEntry item = data.get(position);

        if (item instanceof NavigationDrawerItemAndImg) {
            final ItemAndImgViewHolder viewHolder = (ItemAndImgViewHolder) holder;
            viewHolder.mTitle.setText(((NavigationDrawerItemAndImg) item).getTitle());
            viewHolder.mImageView.setImageResource(((NavigationDrawerItemAndImg) item).getIconId());

            viewHolder.mTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msniiOnClickHandling(viewHolder.mTitle.getText().toString());
                }
            });
        }

        if (item instanceof NavigationDrawerItem) {
            final ItemViewHolder viewHolder = (ItemViewHolder) holder;
            viewHolder.mTitle.setText(((NavigationDrawerItem) item).getTitle());

            viewHolder.mTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msniOnClickHandling(viewHolder.mTitle.getText().toString());
                }
            });
        }

        if (item instanceof NavigationDrawerToogle) {
            final ToggleViewHolder viewHolder = (ToggleViewHolder) holder;

            //TODO: if title MainScreenConstants.NAVIGATION_TOOGLE then
            // I must load the Network connection status and if connected
            // then toogle is Checked else is not checked

            viewHolder.mTitle.setText(((NavigationDrawerToogle) item).getTitle());
            viewHolder.mSwitch.setChecked(((NavigationDrawerToogle) item).isChecked());
            viewHolder.mSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msntOnClickHandling(viewHolder.mTitle.getText().toString(), viewHolder.mSwitch);
                }
            });
        }
    }

    //main_screen_navigation_drawer_item_and_img
    public void msniiOnClickHandling(String title) {
        Log.i(LOG, "msniiOnClickHandling -> ENTER");

        switch (title) {
            case MainScreenConstants.NAVIGATION_PROFILE:
                Log.i(LOG, "-> start my profile");
                //TODO: start the Profile activity
                Intent settingsProfileActivityIntent = SettingsProfileActivity.makeIntent(mContext);
//                addContactActivityIntent.putExtra(,);
                settingsProfileActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(settingsProfileActivityIntent);
                break;
            case MainScreenConstants.NAVIGATION_NETWORK:
                Log.i(LOG, "-> start network");
                //TODO: start the Network activity
                Intent settingsNetworkActivityIntent = SettingsNetworkActivity.makeIntent(mContext);
//                addContactActivityIntent.putExtra(,);
                settingsNetworkActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(settingsNetworkActivityIntent);
                break;
            case MainScreenConstants.NAVIGATION_STORAGE:
                Log.i(LOG, "-> start storage");
                //TODO: start the Storage activity
                Intent settingsStorageActivity = SettingsStorageActivity.makeIntent(mContext);