package space.animefu.starfallhaven;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.widget.Toast;

public final class MainActivity extends Activity implements HavenView.Host,RegionView.Host {
    private HavenView view; private RegionView region; private GameWorld world; private GameSaveStore saves; private GameAudioManager audio; private ChronicleClient chronicle;
    @Override public void onCreate(Bundle state){
        super.onCreate(state);
        configureEdgeToEdge();
        saves=new GameSaveStore(this);audio=new GameAudioManager(this);chronicle=new ChronicleClient();
        try{world=saves.load();if(world==null)world=new GameWorld(84621L);}catch(Exception e){world=new GameWorld(84621L);Toast.makeText(this,"Save could not be loaded; starting a fresh haven",Toast.LENGTH_LONG).show();}
        if(world.landingSiteConfirmed)enterColony(world);else openRegion(world);
    }
    private void configureEdgeToEdge(){
        Window window=getWindow();
        window.setStatusBarColor(Color.TRANSPARENT);window.setNavigationBarColor(Color.TRANSPARENT);
        if(Build.VERSION.SDK_INT>=30)window.setDecorFitsSystemWindows(false);
        else window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    @Override public void onWindowFocusChanged(boolean focus){super.onWindowFocusChanged(focus);if(focus&&Build.VERSION.SDK_INT>=30){WindowInsetsController c=getWindow().getInsetsController();if(c!=null)c.hide(WindowInsets.Type.statusBars());}}
    @Override public void onResume(){super.onResume();if(audio!=null)audio.resume();if(view!=null&&view.getParent()!=null)view.resume();}
    @Override public void onPause(){if(view!=null)view.pause();if(world!=null)save(world,false);if(audio!=null)audio.pause();super.onPause();}
    @Override public void onStop(){if(world!=null)save(world,false);super.onStop();}
    @Override public void onDestroy(){if(chronicle!=null)chronicle.close();if(audio!=null)audio.release();super.onDestroy();}
    @Override public void save(GameWorld world,boolean explicit){try{saves.save(world);if(explicit)runOnUiThread(()->Toast.makeText(this,"Colony saved",Toast.LENGTH_SHORT).show());}catch(Exception e){runOnUiThread(()->Toast.makeText(this,"Could not save colony",Toast.LENGTH_LONG).show());}}
    @Override public void openRegion(GameWorld world){this.world=world;if(view!=null)view.pause();save(world,false);region=new RegionView(this,world,this);setContentView(region);region.requestApplyInsets();}
    @Override public void enterColony(GameWorld world){this.world=world;region=null;view=new HavenView(this,world,this,audio,new TutorialController(this));setContentView(view);view.requestApplyInsets();if(!isFinishing())view.resume();}
    @Override public boolean narrate(ChronicleClient.Request request,ChronicleClient.Callback callback){return chronicle!=null&&chronicle.narrate(request,callback);}
}
