package app.fedilab.android.drawers;
/* 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.os.AsyncTask;
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.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

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.asynctasks.ManageFiltersAsyncTask;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Filters;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnFilterActionInterface;
import es.dmoral.toasty.Toasty;


/**
 * Created by Thomas on 05/09/2018.
 * Adapter for filters
 */
public class FilterAdapter extends BaseAdapter implements OnFilterActionInterface {

    private List<Filters> filters;
    private Context context;
    private FilterAdapter filterAdapter;
    private RelativeLayout textviewNoAction;

    public FilterAdapter(List<Filters> filters, RelativeLayout textviewNoAction) {
        this.filters = filters;
        this.filterAdapter = this;
        this.textviewNoAction = textviewNoAction;
    }

    @Override
    public int getCount() {
        return filters.size();
    }

    @Override
    public Object getItem(int position) {
        return filters.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        final Filters filter = filters.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.drawer_filters, parent, false);
            holder = new ViewHolder();
            holder.filter_word = convertView.findViewById(R.id.filter_word);
            holder.filter_context = convertView.findViewById(R.id.filter_context);
            holder.edit_filter = convertView.findViewById(R.id.edit_filter);
            holder.delete_filter = convertView.findViewById(R.id.delete_filter);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.filter_word.setText(filter.getPhrase());
        StringBuilder contextString = new StringBuilder();
        if (filter.getContext() != null)
            for (String ct : filter.getContext())
                contextString.append(ct).append(" ");
        holder.filter_context.setText(contextString.toString());
        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;
        }
        holder.edit_filter.setOnClickListener(v -> {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, style);
            LayoutInflater inflater = ((BaseMainActivity) context).getLayoutInflater();
            View dialogView = inflater.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(context),
                    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<?> parent1, View view, int position1, long id) {
                    switch (position1) {
                        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<?> parent1) {
                }
            });

            add_phrase.setText(filter.getPhrase());
            if (filter.getContext() != null)
                for (String val : filter.getContext()) {
                    switch (val) {
                        case "home":
                            context_home.setChecked(true);
                            break;
                        case "public":
                            context_public.setChecked(true);
                            break;
                        case "notifications":
                            context_notification.setChecked(true);
                            break;
                        case "thread":
                            context_conversation.setChecked(true);
                            break;
                    }
                }
            context_whole_word.setChecked(filter.isWhole_word());
            context_drop.setChecked(filter.isIrreversible());

            dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> {

                if (add_phrase.getText() != null && add_phrase.getText().toString().trim().length() > 0) {
                    Filters filterSent = new Filters();
                    filterSent.setId(filter.getId());
                    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");
                    filterSent.setContext(contextFilter);
                    filterSent.setExpires_in(expire[0]);
                    filterSent.setPhrase(add_phrase.getText().toString());
                    filterSent.setWhole_word(context_whole_word.isChecked());
                    filterSent.setIrreversible(context_drop.isChecked());
                    new ManageFiltersAsyncTask(context, ManageFiltersAsyncTask.action.UPDATE_FILTER, filterSent, FilterAdapter.this).execute();
                }

                dialog.dismiss();
            });
            dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());


            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.setTitle(context.getString(R.string.action_update_filter));
            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();
        });
        holder.delete_filter.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
            builder.setTitle(context.getString(R.string.action_filter_delete));
            builder.setMessage(context.getString(R.string.action_lists_confirm_delete));
            builder.setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton(R.string.yes, (dialog, which) -> {
                        filters.remove(filter);
                        filterAdapter.notifyDataSetChanged();
                        new ManageFiltersAsyncTask(context, ManageFiltersAsyncTask.action.DELETE_FILTER, filter, FilterAdapter.this).execute();
                        if (filters.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
                            textviewNoAction.setVisibility(View.VISIBLE);
                        dialog.dismiss();
                    })
                    .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
                    .show();
        });
        return convertView;
    }


    private Filters getItemAt(int position) {
        if (filters.size() > position)
            return filters.get(position);
        else
            return null;
    }

    @Override
    public void onActionDone(ManageFiltersAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
        if (apiResponse != null) {
            if (apiResponse.getError() != null) {
                Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
                return;
            }
            List<Filters> filtersRes = apiResponse.getFilters();
            if (filtersRes != null && filtersRes.size() > 0) {
                notifyStatusChanged(filtersRes.get(0));
            }
        }
    }

    private void notifyStatusChanged(Filters filter) {
        for (int i = 0; i < filterAdapter.getCount(); i++) {
            //noinspection ConstantConditions
            if (filterAdapter.getItemAt(i) != null && filterAdapter.getItemAt(i).getId().equals(filter.getId())) {
                filters.set(i, filter);
                try {
                    filterAdapter.notifyDataSetChanged();
                } catch (Exception ignored) {
                }
                return;
            }
        }
    }

    private static class ViewHolder {
        TextView filter_word;
        TextView filter_context;
        FloatingActionButton edit_filter;
        FloatingActionButton delete_filter;
    }


}