package com.foxx.androidcast;

/*********************************************************************
 * DrawerHostActivity.java
 * Created at: Sun 17 May 2026 18:17:14 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (3 commits, 127 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	e92beee804af837a428b8585f08940622843118e465cb20652c06d90785ef2d9
**********************************************************************/
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

/**
 * Hosts main content and a start-edge settings drawer.
 * TV / remote: tap or drag the left-edge handle, or press DPAD_LEFT, to open settings.
 */
public abstract class DrawerHostActivity extends AppCompatActivity {
    private static final float HANDLE_DRAG_OPEN_PX = 48f;

    private DrawerLayout drawerLayout;
    private View settingsHandle;
    private float handleDragStartX;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CastLocaleHelper.attach(newBase));
    }

    protected void applyActivityTheme() {
        CastThemeHelper.applyTheme(this);
    }

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

        drawerLayout = findViewById(R.id.drawer_layout);
        android.view.ViewGroup contentRoot = findViewById(R.id.content_root);
        getLayoutInflater().inflate(getContentLayoutId(), contentRoot, true);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.settings_drawer_container, new CastSettingsFragment())
                    .commit();
        }

        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        wireSettingsHandle();
        getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    setEnabled(false);
                    getOnBackPressedDispatcher().onBackPressed();
                }
            }
        });
    }

    private void wireSettingsHandle() {
        settingsHandle = findViewById(R.id.settings_drawer_handle);
        if (settingsHandle == null) {
            return;
        }
        settingsHandle.setOnClickListener(v -> openSettingsDrawer());
        settingsHandle.setOnTouchListener((v, event) -> {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    handleDragStartX = event.getRawX();
                    return false;
                case MotionEvent.ACTION_UP:
                    if (event.getRawX() - handleDragStartX >= HANDLE_DRAG_OPEN_PX) {
                        openSettingsDrawer();
                        return true;
                    }
                    return false;
                default:
                    return false;
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                && drawerLayout != null
                && !drawerLayout.isDrawerOpen(GravityCompat.START)) {
            openSettingsDrawer();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

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

    protected void openSettingsDrawer() {
        if (drawerLayout != null) {
            drawerLayout.openDrawer(GravityCompat.START);
        }
    }

    protected void closeSettingsDrawer() {
        if (drawerLayout != null) {
            drawerLayout.closeDrawer(GravityCompat.START);
        }
    }

    protected abstract @LayoutRes int getContentLayoutId();
}
