/**
 *   ownCloud Android client application
 *
 *   @author David A. Velasco
 *   Copyright (C) 2015 ownCloud Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2,
 *   as published by the Free Software Foundation.
 *
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.owncloud.android.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.nextcloud.client.di.Injectable;
import com.owncloud.android.R;
import com.owncloud.android.databinding.EditBoxDialogBinding;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.ui.activity.ComponentsGetter;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.theme.ThemeButtonUtils;
import com.owncloud.android.utils.theme.ThemeColorUtils;
import com.owncloud.android.utils.theme.ThemeTextInputUtils;

import javax.inject.Inject;

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

/**
 * Dialog to input the name for a new folder to create.
 * <p>
 * Triggers the folder creation when name is confirmed.
 */
public class CreateFolderDialogFragment
    extends DialogFragment implements DialogInterface.OnClickListener, Injectable {

    private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";

    public static final String CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT";

    @Inject ThemeColorUtils themeColorUtils;
    @Inject ThemeButtonUtils themeButtonUtils;
    @Inject ThemeTextInputUtils themeTextInputUtils;

    private OCFile mParentFolder;

    /**
     * Public factory method to create new CreateFolderDialogFragment instances.
     *
     * @param parentFolder Folder to create
     * @return Dialog ready to show.
     */
    public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
        CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
        frag.setArguments(args);
        return frag;

    }

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

        AlertDialog alertDialog = (AlertDialog) getDialog();

        themeButtonUtils.themeBorderlessButton(themeColorUtils,
                                               alertDialog.getButton(AlertDialog.BUTTON_POSITIVE),
                                               alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL));
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int primaryColor = themeColorUtils.primaryColor(getActivity());
        mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);

        // Inflate the layout for the dialog
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        EditBoxDialogBinding binding = EditBoxDialogBinding.inflate(inflater, null, false);
        View view = binding.getRoot();

        // Setup layout
        binding.userInput.setText("");
        binding.userInput.requestFocus();
        themeTextInputUtils.colorTextInput(binding.userInputContainer, binding.userInput, primaryColor);

        // Build the dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view)
                .setPositiveButton(R.string.folder_confirm_create, this)
                .setNeutralButton(R.string.common_cancel, this)
                .setTitle(R.string.uploader_info_dirname);
        AlertDialog d = builder.create();

        Window window = d.getWindow();
        if (window != null) {
            window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }

        return d;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == AlertDialog.BUTTON_POSITIVE) {
            String newFolderName =
                    ((TextView)(getDialog().findViewById(R.id.user_input)))
                        .getText().toString().trim();

            if (TextUtils.isEmpty(newFolderName)) {
                DisplayUtils.showSnackMessage(getActivity(), R.string.filename_empty);
                return;
            }

            if (!FileUtils.isValidName(newFolderName)) {
                DisplayUtils.showSnackMessage(getActivity(), R.string.filename_forbidden_charaters_from_server);

                return;
            }

            String path = mParentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
            ((ComponentsGetter) getActivity()).getFileOperationsHelper().createFolder(path);
        }
    }
}
