package com.wismna.geoffroy.donext.activities;

import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.adapters.TaskRecyclerViewAdapter;
import com.wismna.geoffroy.donext.database.TaskDataAccess;
import com.wismna.geoffroy.donext.fragments.TodayFormDialogFragment;

import org.joda.time.LocalDate;

import java.util.Locale;
import java.util.Objects;

public class TodayActivity extends ToolBarActivityBase
    implements TodayFormDialogFragment.TodayTaskListener {

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initToolBar();

        // Show the date
        TextView dateTextView = findViewById(R.id.today_date);
        dateTextView.setText(LocalDate.now().toString("EEEE, dd MMMM yyyy", getCurrentLocale()));

    }

    @Override
    protected void onStart() {
        super.onStart();

        // Set the no tasks texts
        TextView noTasksTextView = findViewById(R.id.no_more_tasks);
        noTasksTextView.setText(R.string.today_no_tasks);
        noTasksTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_smiley_satisfied_light, 0);
        noTasksTextView.setCompoundDrawablePadding(10);

        TextView createTasksTextView = findViewById(R.id.create_tasks);
        createTasksTextView.setText(R.string.today_create_tasks);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_today, menu);
        return true;
    }

    @Override
    public void onTodayTaskDialogPositiveClick(View dialogView) {
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setEnabled(false);
    }

    @Override
    public void onTodayTasksUpdated() {
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setEnabled(true);
        try (TaskDataAccess taskDataAccess = new TaskDataAccess(this)) {
            RecyclerView recyclerView = findViewById(R.id.task_list_view);
            ((TaskRecyclerViewAdapter)Objects.requireNonNull(recyclerView.getAdapter())).setItems(taskDataAccess.getTodayTasks());
        }
    }

    public void onNewTaskClick(View view) {
        TodayFormDialogFragment taskDialogFragment =
                TodayFormDialogFragment.newInstance(TodayActivity.this);

        FragmentManager fragmentManager = getSupportFragmentManager();

        taskDialogFragment.showFragment(fragmentManager, getString(R.string.action_today_select), getResources().getBoolean(R.bool.large_layout));
    }


    private Locale getCurrentLocale(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            return getResources().getConfiguration().getLocales().get(0);
        } else{
            //noinspection deprecation
            return getResources().getConfiguration().locale;
        }
    }
}
