package acquire.app;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.IdRes;

import java.util.concurrent.CountDownLatch;

import acquire.app.databinding.AppActivityMainBinding;
import acquire.app.fragment.main.MainFragment;
import acquire.app.fragment.splash.SplashFragment;
import acquire.base.BaseApplication;
import acquire.base.activity.BaseActivity;
import acquire.base.activity.callback.SimpleCallback;
import acquire.base.utils.DisplayUtils;
import acquire.core.tools.SelfCheckHelper;
import acquire.core.utils.AuxScreenManager;
import acquire.sdk.DeviceHelper;
import acquire.sdk.ServiceHelper;
import acquire.sdk.system.BSystem;


/**
 * The main Activity
 *
 * @author Janson
 * @date 2018/10/4 18:13
 */
public class MainActivity extends BaseActivity {

    @Override
    public @IdRes
    int attachFragmentResId() {
        return R.id.fragment_layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppActivityMainBinding binding = AppActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());
        //set immersed status bar.
        DisplayUtils.immersedStatusBar(getWindow());
        splashAnimation();
        /*
         * must be executed after SelfCheckHelper.initAppConfig(context) in App.class,
         * so use BaseApplication.SINGLE_EXECUTOR
         */
        BaseApplication.SINGLE_EXECUTOR.execute(() -> {
            // Register a one-shot listener so the AUX LCD logo is pushed as soon
            // as the device service is bound — even if that happens after initDevice()
            // returns (async binding on cold boot).
            DeviceHelper.addOnDeviceReadyListener(() -> AuxScreenManager.showLogo(MainActivity.this));
            SelfCheckHelper.initDevice(this);
            //disable task and home button
            BSystem.setTaskButton(false);
            BSystem.setHomeButton(false);
            //Wait for the splash animation to end
            splashWaitFinish();
            //Enter the main fragment
            // Initially display the HomeFragment instead of MainFragment
            mSupportDelegate.switchContent(MainFragment.newInstance());
          //  runOnUiThread(() -> mSupportDelegate.switchContent(HomescreenFragment.newInstance()));
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Restore the AUX LCD logo whenever the app returns to the foreground
        // (e.g., after a system dialog or after the app is backgrounded).
        // Navigation-back within the app is handled by MainFragment.onResume().
        AuxScreenManager.showLogo(this);
    }

    @Override
    protected void onDestroy() {
        if (ServiceHelper.getInstance().isInit()) {
            //restore task and home button
            BSystem.setTaskButton(true);
            BSystem.setHomeButton(true);
        }

        super.onDestroy();
    }

    private static CountDownLatch splashLatch;

    private void splashAnimation() {
        if (splashLatch == null) {
            splashLatch = new CountDownLatch(1);
            mSupportDelegate.switchContent(SplashFragment.newInstance(new SimpleCallback() {
                @Override
                public void result() {
                    splashLatch.countDown();
                }
            }));
        } else {
            splashLatch.countDown();
        }
    }

    private void splashWaitFinish() {
        if (splashLatch != null) {
            try {
                splashLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
