package com.example.mynotepad;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.room.Room;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.example.mynotepad.MenuFeatures.AllArchivedNotes.ArchivedNotesActivity;
import com.example.mynotepad.MenuFeatures.AllNotes.AllNotesActivity;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.NoteDataBase;
import com.example.mynotepad.MenuFeatures.Calender.MyCalenderActivity;
import com.example.mynotepad.MenuFeatures.Pics.PicsActivity;
import com.example.mynotepad.MenuFeatures.Setting.SettingActivity;
import com.example.mynotepad.MenuFeatures.Utils;
import com.example.mynotepad.MenuFeatures.VoiceMassages.VoiceMassagesActivity;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    public static NoteDataBase noteDataBase;
    private DrawerLayout drawerLayout;
    private ImageView theme;
    private NavigationView navigationView;
    private SharedPreferencesClass sharedPreferencesClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //todo check prefreces and set primitive theme and it is enough to set sTheme
        super.onCreate(savedInstanceState);
        sharedPreferencesClass = new SharedPreferencesClass();
        sharedPreferencesClass.getThemeOnMainActivityCreate(this);
        Utils.onActivityCreateThemeForMainActivity(this);
        setContentView(R.layout.activity_main);

        init();
        setBg(Utils.sTheme);
        addListener();
    }

    private void setBg(String sTheme) {
        switch (sTheme) {
            case "dark":
                drawerLayout.setBackgroundResource(R.drawable.main_bg_dark);
                break;
            case "bright":
                drawerLayout.setBackgroundResource(R.drawable.main_bg_bright);
                break;
        }
    }

    private void init() {
        noteDataBase = Room.databaseBuilder(this, NoteDataBase.class, "RoomDb").fallbackToDestructiveMigration().allowMainThreadQueries().build();

        drawerLayout = findViewById(R.id.drawerLayout);
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        navigationView = findViewById(R.id.main_menu);//menu drawer in ui
        navigationView.setNavigationItemSelectedListener(this);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.setDrawerIndicatorEnabled(true);
        toggle.syncState();

        drawerLayout.addDrawerListener(toggle);

        View header = navigationView.getHeaderView(0);
        theme = header.findViewById(R.id.theme);
        if (Utils.sTheme.equals("dark")){
            theme.setImageResource(R.drawable.ic_sun);
        }else {
            theme.setImageResource(R.drawable.ic_night_mode);
        }
    }

    private void addListener() {
        theme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.sTheme.equals("bright")) {
                    changeThemeToDark();
                } else if (Utils.sTheme.equals("dark")) {
                    changeThemeToBright();
                }
            }
        });
    }

    public void allNoteClicked(View view) {
        startActivity(new Intent(getApplicationContext(), AllNotesActivity.class));
    }

    public void onCalendarClicked(View view) {
        startActivity(new Intent(getApplicationContext(), MyCalenderActivity.class));
    }

    public void onVoiceMassagesClicked(View view) {
        startActivity(new Intent(getApplicationContext(), VoiceMassagesActivity.class));
    }

    public void noNotedPicsClicked(View view) {
        startActivity(new Intent(getApplicationContext(), PicsActivity.class));
    }

    public void onProgramsClicked(View view) {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        switch (id) {
            case R.id.favorites: {
                startActivity(new Intent(getApplicationContext(), ArchivedNotesActivity.class));
                break;
            }
            case R.id.folders: {
                break;
            }
            case R.id.settings: {
                startActivity(new Intent(getApplicationContext(), SettingActivity.class));
            }
            case R.id.information: {
            }
        }
        return true;
    }

    private void changeThemeToDark() {
        animations(theme);
        Utils.changeToTheme(this, "dark");
        sharedPreferencesClass.changeTheTheme(this);
    }

    private void changeThemeToBright() {
        animations(theme);
        Utils.changeToTheme(this, "bright");
        sharedPreferencesClass.changeTheTheme(this);
    }


    private void animations(ImageView imageView) {
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
        alpha.setDuration(1000).start();
        rotation.setDuration(1000).start();
    }
}