package me.paladin.notification.activity;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import me.paladin.notification.R;
import me.paladin.notification.fragment.FragmentAction;
import me.paladin.notification.fragment.FragmentContent;
import me.paladin.notification.fragment.FragmentSchedule;

public class AddActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private FragmentManager manager;
    private FragmentContent content;
    private FragmentAction action;
    private FragmentSchedule schedule;
    private Fragment active;
    BottomNavigationView navView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        manager = getSupportFragmentManager();
        navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(this);
        content = (FragmentContent) manager.findFragmentByTag("Content");
        if (content == null) {
            content = new FragmentContent();
            action = new FragmentAction();
            schedule = new FragmentSchedule();
            active = content;
            manager.beginTransaction().add(R.id.addContainer, content, "Content").commit();
            manager.beginTransaction().add(R.id.addContainer, action, "Action").hide(action).commit();
            manager.beginTransaction().add(R.id.addContainer, schedule, "Schedule").hide(schedule).commit();
        } else {
            action = (FragmentAction) manager.findFragmentByTag("Action");
            schedule = (FragmentSchedule) manager.findFragmentByTag("Schedule");
        }
    }
    
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("SelectedItemId", navView.getSelectedItemId());
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int selectedId = savedInstanceState.getInt("SelectedItemId");
        navView.setSelectedItemId(selectedId);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_toolbar, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
        } else if (id == R.id.item_add_create) {
            Toast.makeText(this, "Title: " + content.getNotificationModel().getTitle() + "\nMessage: " + content.getNotificationModel().getMessage(), Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = null;
        if (id == R.id.itemBottomContent) {
            fragment = content;
        } else if (id == R.id.itemBottomAction) {
            fragment = action;
        } else if (id == R.id.itemBottomSchedule) {
            fragment = schedule;
        }
        if (active == null) active = fragment;
        if (fragment != null && fragment != active) {
            manager.beginTransaction().hide(active).show(fragment).commit();
            active = fragment;
            return true;
        }
        return false;
    }
}
