package in.testpress.testpress.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import in.testpress.testpress.Injector;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import javax.inject.Inject;

import butterknife.ButterKnife;
import in.testpress.testpress.R;
import in.testpress.testpress.TestpressApplication;
import in.testpress.testpress.TestpressServiceProvider;
import in.testpress.testpress.authenticator.LogoutService;
import in.testpress.testpress.core.Constants;
import in.testpress.testpress.core.TestpressService;
import in.testpress.testpress.events.CustomErrorEvent;
import in.testpress.testpress.events.UnAuthorizedUserErrorEvent;
import in.testpress.testpress.models.DaoSession;
import in.testpress.testpress.models.InstituteSettings;
import in.testpress.testpress.models.InstituteSettingsDao;
import in.testpress.testpress.util.Ln;
import in.testpress.testpress.util.UIUtils;
import in.testpress.ui.UserDevicesActivity;
import io.sentry.Sentry;

import static in.testpress.testpress.BuildConfig.BASE_URL;


/**
 * Base class for all Testpress Activities that need fragments.
 */
public class TestpressFragmentActivity extends AppCompatActivity {

    @Inject
    protected Bus eventBus;
    @Inject protected TestpressServiceProvider serviceProvider;
    @Inject protected TestpressService testpressService;
    @Inject protected LogoutService logoutService;

    protected Toolbar mActionBarToolbar;
    protected Object busEventListener, unauthorisedUserErrorBusListener;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Injector.inject(this);

        // Directly subscribing in parent class won't work, only child class subscribers will work. https://github.com/square/otto/issues/26
        busEventListener = new Object() {
            @Subscribe
            public void onCustomErrorEvent(CustomErrorEvent customErrorEvent) {
                TestpressFragmentActivity.this.onReceiveCustomErrorEvent(customErrorEvent);
            }
        };

        unauthorisedUserErrorBusListener = new Object() {
            @Subscribe
            public void onUnAuthorizedUserErrorEvent(UnAuthorizedUserErrorEvent unAuthorizedUserErrorEvent) {
                try {
                    serviceProvider.logout(TestpressFragmentActivity.this, testpressService, serviceProvider, logoutService);
                } catch (Exception e) {
                    Ln.e("Exception : " + e.getLocalizedMessage());
//                    Sentry.capture(e);
                }
            }
        };


        eventBus.register(busEventListener);
        eventBus.register(unauthorisedUserErrorBusListener);
    }

    public Toolbar getActionBarToolbar() {
        if (mActionBarToolbar == null) {
            mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
            if (mActionBarToolbar != null) {
                setSupportActionBar(mActionBarToolbar);
            }
        }
        return mActionBarToolbar;
    }

    @Override
    public void setContentView(final int layoutResId) {
        super.setContentView(layoutResId);

        ButterKnife.inject(this);
        Toolbar toolbar = getActionBarToolbar();
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            if (getIntent().getBooleanExtra(Constants.IS_DEEP_LINK, false)) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            } else {
                onBackPressed();
            }
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        try {
            super.onBackPressed();
        } catch (IllegalStateException e) {
            supportFinishAfterTransition();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        eventBus.register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        eventBus.unregister(this);
    }

    protected void onReceiveCustomErrorEvent(final CustomErrorEvent customErrorEvent) {
        if (customErrorEvent.getErrorCode().equals(getString(R.string.PARALLEL_LOGIN_RESTRICTION_ERROR_CODE))) {
            Intent intent = new Intent(this, UserDevicesActivity.class);
            startActivity(intent);
        } else if (customErrorEvent.getErrorCode().equals(getString(R.string.MAX_LOGIN_EXCEEDED_ERROR_CODE))) {
            DaoSession daoSession = TestpressApplication.getDaoSession();
            InstituteSettingsDao instituteSettingsDao = daoSession.getInstituteSettingsDao();
            InstituteSettings instituteSettings = instituteSettingsDao.queryBuilder()
                    .where(InstituteSettingsDao.Properties.BaseUrl.eq(BASE_URL))
                    .list().get(0);

            String message = getString(R.string.max_login_limit_exceeded_error);

            if (instituteSettings.getCooloffTime() != null) {
                message += getString(R.string.account_unlock_info) + " %s hours";
                message = String.format(message, instituteSettings.getCooloffTime());
            }

            try {
                in.testpress.util.UIUtils.showAlert(TestpressFragmentActivity.this, "Account Locked", message);
            } catch (Exception e) {
                Ln.e("Exception : " + e.getLocalizedMessage());
//                Sentry.capture(e);
            }
        }
    }
}

@@ -506,16 +506,40 @@
 cribe;%0A%0A
+import java.util.List;%0A%0A
 import j
@@ -5046,32 +5046,54 @@
 ctivity.class);%0A
+            try %7B%0A    
             star
@@ -5103,32 +5103,74 @@
 tivity(intent);%0A
+            %7D catch (Exception ignore) %7B%7D%0A
         %7D else i
@@ -5437,32 +5437,37 @@
 ();%0A            
+List%3C
 InstituteSetting
@@ -5467,16 +5467,17 @@
 Settings
+%3E
  institu
@@ -5486,16 +5486,20 @@
 Settings
+List
  = insti
@@ -5641,15 +5641,8 @@
 st()
-.get(0)
 ;%0A%0A 
@@ -5718,24 +5718,165 @@
 ed_error);%0A%0A
+            if (instituteSettingsList.size() %3E 0) %7B%0A                InstituteSettings instituteSettings = instituteSettingsList.get(0);%0A%0A    
             
@@ -5917,32 +5917,36 @@
 me() != null) %7B%0A
+    
                 
@@ -6019,32 +6019,36 @@
                 
+  
+  
 message = String
@@ -6101,16 +6101,34 @@
 ime());%0A
+                %7D%0A
         

Handle case if instituteSettings is not present