/* Copyright 2017 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */
package app.fedilab.android.activities;


import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import app.fedilab.android.R;
import app.fedilab.android.drawers.TagsEditAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TagsCacheDAO;
import es.dmoral.toasty.Toasty;


/**
 * Created by Thomas on 01/12/2018.
 * Tag cache activity class
 */

public class TagCacheActivity extends BaseActivity {

    private RecyclerView tag_list;
    private List<String> tags;
    private TagsEditAdapter tagsEditAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
        int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
        if (theme == Helper.THEME_LIGHT) {
            setTheme(R.style.Dialog);
        } else {
            setTheme(R.style.DialogDark);
        }
        getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        tags = new ArrayList<>();
        setContentView(R.layout.activity_tag_cache);

        tag_list = findViewById(R.id.tag_list);
        SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
        EditText tag_add = findViewById(R.id.tag_add);
        ImageButton save_tag = findViewById(R.id.save_tag);

        save_tag.setOnClickListener(v -> {
            if (tag_add.getText() != null && tag_add.getText().toString().trim().replaceAll("#", "").length() > 0) {
                String tagToInsert = tag_add.getText().toString().trim().replaceAll("#", "");
                boolean isPresent = new TagsCacheDAO(TagCacheActivity.this, db).isPresent(tagToInsert);
                if (isPresent)
                    Toasty.warning(TagCacheActivity.this, getString(R.string.tags_already_stored), Toast.LENGTH_LONG).show();
                else {
                    new TagsCacheDAO(TagCacheActivity.this, db).insert(tagToInsert);
                    int position = tags.size();
                    tags.add(tagToInsert);
                    Toasty.success(TagCacheActivity.this, getString(R.string.tags_stored), Toast.LENGTH_LONG).show();
                    tag_add.setText("");
                    tagsEditAdapter.notifyItemInserted(position);
                }
            }
        });

        setTitle(R.string.manage_tags);
        AsyncTask.execute(() -> {

            List<String> tagsTemp = new TagsCacheDAO(TagCacheActivity.this, db).getAll();
            if (tagsTemp != null)
                tags = tagsTemp;
            if (tags != null) {
                tagsEditAdapter = new TagsEditAdapter(tags);
                tag_list.setAdapter(tagsEditAdapter);
                LinearLayoutManager mLayoutManager = new LinearLayoutManager(TagCacheActivity.this);
                tag_list.setLayoutManager(mLayoutManager);
            }
        });

    }
}
