package app.fedilab.android.fragments;
/* Copyright 2018 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>. */

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

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

import app.fedilab.android.R;
import app.fedilab.android.activities.BaseMainActivity;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.ManageFiltersAsyncTask;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Filters;
import app.fedilab.android.client.Entities.ManageTimelines;
import app.fedilab.android.drawers.FilterAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnFilterActionInterface;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TimelinesDAO;
import es.dmoral.toasty.Toasty;

import static app.fedilab.android.activities.BaseMainActivity.mPageReferenceMap;


/**
 * Created by Thomas on 05/09/2018.
 * Fragment to display Filters
 */
public class DisplayFiltersFragment extends Fragment implements OnFilterActionInterface {


    private Context context;
    private AsyncTask<Void, Void, Void> asyncTask;

    private RelativeLayout mainLoader;
    private FloatingActionButton add_new;
    private FilterAdapter filterAdapter;
    private RelativeLayout textviewNoAction;
    private ListView lv_filters;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //View for fragment is the same that fragment accounts
        View rootView = inflater.inflate(R.layout.fragment_filters, container, false);

        context = getContext();


        lv_filters = rootView.findViewById(R.id.lv_filters);
        textviewNoAction = rootView.findViewById(R.id.no_action);
        mainLoader = rootView.findViewById(R.id.loader);
        RelativeLayout nextElementLoader = rootView.findViewById(R.id.loading_next_items);
        mainLoader.setVisibility(View.VISIBLE);
        nextElementLoader.setVisibility(View.GONE);
        filterAdapter = new FilterAdapter(BaseMainActivity.filters, textviewNoAction);
        lv_filters.setAdapter(filterAdapter);
        asyncTask = new ManageFiltersAsyncTask(context, ManageFiltersAsyncTask.action.GET_ALL_FILTER, null, DisplayFiltersFragment.this).execute();
        try {
            add_new = ((MainActivity) context).findViewById(R.id.add_new);
        } catch (Exception ignored) {
        }
        if (add_new != null)
            add_new.setOnClickListener(view -> {
                final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
                int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
                int style;
                if (theme == Helper.THEME_DARK) {
                    style = R.style.DialogDark;
                } else if (theme == Helper.THEME_BLACK) {
                    style = R.style.DialogBlack;
                } else {
                    style = R.style.Dialog;
                }
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, style);
                LayoutInflater inflater1 = getLayoutInflater();
                View dialogView = inflater1.inflate(R.layout.add_filter, new LinearLayout(context), false);
                dialogBuilder.setView(dialogView);

                EditText add_phrase = dialogView.findViewById(R.id.add_phrase);
                CheckBox context_home = dialogView.findViewById(R.id.context_home);
                CheckBox context_public = dialogView.findViewById(R.id.context_public);
                CheckBox context_notification = dialogView.findViewById(R.id.context_notification);
                CheckBox context_conversation = dialogView.findViewById(R.id.context_conversation);
                CheckBox context_whole_word = dialogView.findViewById(R.id.context_whole_word);
                CheckBox context_drop = dialogView.findViewById(R.id.context_drop);
                Spinner filter_expire = dialogView.findViewById(R.id.filter_expire);
                ArrayAdapter<CharSequence> adapterResize = ArrayAdapter.createFromResource(Objects.requireNonNull(getActivity()),
                        R.array.filter_expire, android.R.layout.simple_spinner_dropdown_item);
                filter_expire.setAdapter(adapterResize);
                final int[] expire = {-1};
                filter_expire.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                expire[0] = -1;
                                break;
                            case 1:
                                expire[0] = 3600;
                                break;
                            case 2:
                                expire[0] = 21600;
                                break;
                            case 3:
                                expire[0] = 43200;
                                break;
                            case 4:
                                expire[0] = 86400;
                                break;
                            case 5:
                                expire[0] = 604800;
                                break;
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
                dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> {

                    if (add_phrase.getText() != null && add_phrase.getText().toString().trim().length() > 0) {
                        Filters filter = new Filters();
                        ArrayList<String> contextFilter = new ArrayList<>();
                        if (context_home.isChecked())
                            contextFilter.add("home");
                        if (context_public.isChecked())
                            contextFilter.add("public");
                        if (context_notification.isChecked())
                            contextFilter.add("notifications");
                        if (context_conversation.isChecked())
                            contextFilter.add("thread");
                        filter.setContext(contextFilter);
                        filter.setPhrase(add_phrase.getText().toString());
                        filter.setExpires_in(expire[0]);
                        filter.setWhole_word(context_whole_word.isChecked());
                        filter.setIrreversible(context_drop.isChecked());
                        new ManageFiltersAsyncTask(context, ManageFiltersAsyncTask.action.CREATE_FILTER, filter, DisplayFiltersFragment.this).execute();
                    }
                    dialog.dismiss();
                    add_new.setEnabled(false);
                });
                dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());


                AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.setTitle(getString(R.string.action_filter_create));
                alertDialog.setOnDismissListener(dialogInterface -> {
                    //Hide keyboard
                    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    assert imm != null;
                    imm.hideSoftInputFromWindow(add_phrase.getWindowToken(), 0);
                });
                if (alertDialog.getWindow() != null)
                    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                alertDialog.show();
            });
        return rootView;
    }


    @Override
    public void onCreate(Bundle saveInstance) {
        super.onCreate(saveInstance);
    }


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    public void onDestroy() {
        super.onDestroy();
        if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
            asyncTask.cancel(true);
    }


    @Override
    public void onActionDone(ManageFiltersAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
        mainLoader.setVisibility(View.GONE);
        add_new.setEnabled(true);
        if (apiResponse.getError() != null) {
            if (apiResponse.getError().getError().length() < 100) {
                Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
            } else {
                Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
            }
            return;
        }
        if (actionType == ManageFiltersAsyncTask.action.GET_ALL_FILTER) {
            if (apiResponse.getFilters() != null && apiResponse.getFilters().size() > 0) {
                filterAdapter.notifyDataSetChanged();
                textviewNoAction.setVisibility(View.GONE);
                lv_filters.setVisibility(View.VISIBLE);
            } else {
                textviewNoAction.setVisibility(View.VISIBLE);
                lv_filters.setVisibility(View.GONE);
            }
        } else if (actionType == ManageFiltersAsyncTask.action.CREATE_FILTER) {
            if (apiResponse.getFilters() != null && apiResponse.getFilters().size() > 0) {
                BaseMainActivity.filters.add(0, apiResponse.getFilters().get(0));
                filterAdapter = new FilterAdapter(BaseMainActivity.filters, textviewNoAction);
                lv_filters.setAdapter(filterAdapter);
                textviewNoAction.setVisibility(View.GONE);
                lv_filters.setVisibility(View.VISIBLE);
            } else {
                Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
            }
        }
        BaseMainActivity.filters = apiResponse.getFilters();
        SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
        List<ManageTimelines> timelines = new TimelinesDAO(context, db).getDisplayedTimelines();
        if (mPageReferenceMap == null) {
            return;
        }
        for (ManageTimelines tl : timelines) {
            if (tl.getType() == ManageTimelines.Type.HOME || tl.getType() == ManageTimelines.Type.LOCAL || tl.getType() == ManageTimelines.Type.PUBLIC) {
                DisplayStatusFragment displayStatusFragment = (DisplayStatusFragment) mPageReferenceMap.get(tl.getPosition());
                if (displayStatusFragment != null)
                    displayStatusFragment.refreshFilter();
            }
        }

    }

}
