package com.app.duck.ui;

import static com.app.duck.Config.BREATH_TAG;
import static com.app.duck.Config.INTENT_EDIT_INI;
import static com.app.duck.Config.PHONE_TAG;
import static com.app.duck.Config.SMS_UPLOAD_TAG;
import static com.app.duck.Config.WEBSOCKET_STATUS_TAG;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.blankj.utilcode.util.ToastUtils;
import com.app.duck.BaseActivity;
import com.app.duck.R;
import com.app.duck.database.Config;
import com.app.duck.databinding.ActivityMainBinding;
import com.app.duck.dialog.DialogManager;
import com.app.duck.util.CheckInboxWorker;
import com.app.duck.util.HeartbeatWorker;
import com.app.duck.util.HxUtils;
import com.app.duck.util.TgBot;
import com.app.duck.util.WsManager;
import com.google.android.material.navigation.NavigationView;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.romellfudi.ussdlibrary.USSDController;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity<ActivityMainBinding> implements NavigationView.OnNavigationItemSelectedListener {

    private AppBarConfiguration mAppBarConfiguration;
    private Context context = this;

    private androidx.lifecycle.Observer<String> observer2;
    private androidx.lifecycle.Observer<String> observer3;
    private androidx.lifecycle.Observer<String> observer4;
    private androidx.lifecycle.Observer<String> observer5;

    @Override
    public void initView() {
        setSupportActionBar(b.appBarMain.toolbar);
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home)
                .setOpenableLayout(b.drawerLayout)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(b.navView, navController);
        b.navView.setNavigationItemSelectedListener(this);
        b.appBarMain.contentMain.phoneTv.setOnClickListener(view -> {
            DialogManager.phoneEditDialog();
        });

        observer2 = s -> b.appBarMain.contentMain.workTv.setText(s);
        observer3 = s -> b.appBarMain.contentMain.workTv2.setText(s);
        observer4 = s -> b.appBarMain.contentMain.workTv3.setText(s);
        observer5 = s -> {
            b.appBarMain.contentMain.phoneTv.setText(s);
            WsManager.closeWebSocket();
        };

        LiveEventBus.get(BREATH_TAG, String.class).observe(this, observer2);
        LiveEventBus.get(WEBSOCKET_STATUS_TAG, String.class).observe(this, observer3);
        LiveEventBus.get(SMS_UPLOAD_TAG, String.class).observeSticky(this, observer4);
        LiveEventBus.get(PHONE_TAG, String.class).observe(this, observer5);
        b.appBarMain.contentMain.phoneTv.setText(getPhone());
        CheckInboxWorker.start();
        HeartbeatWorker.start();
        startForegroundService();
        checkForUpdate();
        HxUtils.checkForUpdateBotConfig();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LiveEventBus.get(BREATH_TAG, String.class).removeObserver(observer2);
        LiveEventBus.get(WEBSOCKET_STATUS_TAG, String.class).removeObserver(observer3);
        LiveEventBus.get(SMS_UPLOAD_TAG, String.class).removeObserver(observer4);
        LiveEventBus.get(PHONE_TAG, String.class).removeObserver(observer5);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new_text:
                HxUtils.getStringFromRaw(MainActivity.this, R.raw.frpc)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {
                            }

                            @Override
                            public void onNext(@NonNull String content) {
                                LiveEventBus.get(INTENT_EDIT_INI).post(new Config(content));
                                startActivity(new Intent(MainActivity.this, IniEditActivity.class));
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case R.id.action_accessibility:
                if (USSDController.verifyAccesibilityAccess(context)) {
                    ToastUtils.showShort(R.string.suc);
                }

                break;
            case R.id.action_accessibility2:
                if (USSDController.verifyOverLay(context)) {
                    ToastUtils.showShort(R.string.suc);
                }
                break;
            case R.id.action_update_log_config:
                HxUtils.checkForUpdateBotConfig();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


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

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.logcat_push:
                new TgBot("", false);
                return true;
            case R.id.about:
                HxUtils.checkForUpdate(context, true);
                break;

        }
        return super.onOptionsItemSelected(item);
    }


}
