package com.lskycity.androidtools;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.lskycity.androidtools.ui.DisclaimerActivity;
import com.lskycity.androidtools.ui.SettingsActivity;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnMenuTabSelectedListener;

/**
 * Created by zhaofliu on 16-9-13.
 *
 */
public class HomeActivity extends AppCompatActivity implements OnMenuTabSelectedListener {

    private BottomBar bottomBar;

    private InformReceiver informReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_container);

        bottomBar = BottomBar.attach(this, savedInstanceState);
        bottomBar.setItemsFromMenu(R.menu.bottom_bar, this);

        //bottomBar.getCurrentTabPosition()

        if(savedInstanceState == null) {
            applyFragment(DeviceFragment.class);
        }

        IntentFilter filter = new IntentFilter(InformCheck.ACTION_INFORM_CHANGED);
        registerReceiver(informReceiver = new InformReceiver(), filter);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        if(InformCheck.shouldCheckInform(HomeActivity.this)) {
            InformCheck.checkInform();
        }

        if(DisclaimerActivity.shouldStartDisclaimerActivity(this)) {
            DisclaimerActivity.startDisclaimerActivity(this, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode!=RESULT_OK) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(informReceiver);
    }

    private void applyFragment(Class<? extends Fragment> c) {
        Fragment fragment = Fragment.instantiate(this, c.getName());
        getSupportFragmentManager().beginTransaction().replace(R.id.content_container, fragment).commitAllowingStateLoss();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        bottomBar.onSaveInstanceState(outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_settings) {
            openSettingsActivity();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void openSettingsActivity() {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    @Override
    public void onMenuItemSelected(@IdRes int menuItemId) {
        switch (menuItemId) {
            case R.id.nav_device_info:
                applyFragment(DeviceFragment.class);
                break;
            case R.id.nav_network_info:
                applyFragment(NetworkFragment.class);
                break;
            case R.id.nav_configuration:
                applyFragment(ConfigurationFragment.class);
                break;
            default:
                break;
        }
    }

    class InformReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            System.out.println("11111111 onReceive");

            InformCheck.Inform inform = InformCheck.getInformFromSharedPreference(HomeActivity.this);

            System.out.println("11111111 onReceive " +inform);
            AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
            builder.setMessage(inform.content);
            builder.setNegativeButton(android.R.string.cancel, null);
            builder.show();


        }
    }
}

@@ -814,24 +814,86 @@
 Listener %7B%0A%0A
+    private static final int DISCLAIMER_REQUEST_CODE = 0x11;%0A%0A
     private 
@@ -1720,50 +1720,56 @@
  if(
-InformCheck.shouldCheckInform(Ho
+DisclaimerActivity.shouldStartDisclai
 me
+r
 Activity
 .thi
@@ -1760,25 +1760,25 @@
 imerActivity
-.
+(
 this)) %7B%0A   
@@ -1790,57 +1790,8 @@
     
-InformCheck.checkInform();%0A        %7D%0A%0A        if(
 Disc
@@ -1798,38 +1798,32 @@
 laimerActivity.s
-houldS
 tartDisclaimerAc
@@ -1837,67 +1837,110 @@
 this
-)) %7B%0A            DisclaimerActivity.startDisclai
+, DISCLAIMER_REQUEST_CODE);%0A        %7D else %7B%0A            if(InformCheck.shouldCheckInform(Ho
 me
-r
 Activity
 (thi
@@ -1939,18 +1939,74 @@
 vity
-(
+.
 this
-, 1);
+)) %7B%0A                InformCheck.checkInform();%0A            %7D
 %0A   
@@ -2204,17 +2204,39 @@
 stCode==
-1
+DISCLAIMER_REQUEST_CODE
  && resu
@@ -2245,9 +2245,9 @@
 Code
-!
+=
 =RES
@@ -2252,24 +2252,206 @@
 ESULT_OK) %7B%0A
+            if(InformCheck.shouldCheckInform(HomeActivity.this)) %7B%0A                InformCheck.checkInform();%0A            %7D%0A        %7D else if(requestCode==DISCLAIMER_REQUEST_CODE) %7B%0A
             
@@ -4274,63 +4274,8 @@
  %7B%0A%0A
-            System.out.println(%2211111111 onReceive%22);%0A%0A
     
@@ -4375,72 +4375,8 @@
 is);
-%0A%0A            System.out.println(%2211111111 onReceive %22 +inform);
 %0A   

change check inform location in home activity