/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.documentsui.ui;

import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_CONVERTED;
import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_FAILURE;
import static com.android.documentsui.util.Material3Config.getRes;

import android.content.Context;
import android.net.Uri;
import android.text.BidiFormatter;
import android.text.Html;

import androidx.annotation.PluralsRes;

import com.android.documentsui.OperationDialogFragment.DialogType;
import com.android.documentsui.R;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.services.FileOperationService;
import com.android.documentsui.services.FileOperationService.OpType;

import java.io.File;
import java.util.List;

public class MessageBuilder {

    private Context mContext;

    public MessageBuilder(Context context) {
        mContext = context;
    }

    public String generateDeleteMessage(List<DocumentInfo> docs) {
        String message;
        int dirsCount = 0;

        for (DocumentInfo doc : docs) {
            if (doc.isDirectory()) {
                ++dirsCount;
            }
        }

        if (docs.size() == 1) {
            // Deleteing 1 file xor 1 folder in cwd

            // Address b/28772371, where including user strings in message can result in
            // broken bidirectional support.
            String displayName = BidiFormatter.getInstance().unicodeWrap(docs.get(0).displayName);
            message =
                    dirsCount == 0
                            ? mContext.getString(
                                    getRes(R.string.delete_filename_confirmation_message),
                                    displayName)
                            : mContext.getString(
                                    getRes(R.string.delete_foldername_confirmation_message),
                                    displayName);
        } else if (dirsCount == 0) {
            // Deleting only files in cwd
            message =
                    Shared.getQuantityString(
                            mContext,
                            getRes(R.plurals.delete_files_confirmation_message),
                            docs.size());
        } else if (dirsCount == docs.size()) {
            // Deleting only folders in cwd
            message =
                    Shared.getQuantityString(
                            mContext,
                            getRes(R.plurals.delete_folders_confirmation_message),
                            docs.size());
        } else {
            // Deleting mixed items (files and folders) in cwd
            message =
                    Shared.getQuantityString(
                            mContext,
                            getRes(R.plurals.delete_items_confirmation_message),
                            docs.size());
        }
        return message;
    }

    public String generateListMessage(
            @DialogType int dialogType, @OpType int operationType, List<DocumentInfo> docs,
            List<Uri> uris, List<String> paths) {
        int resourceId;

        switch (dialogType) {
            case DIALOG_TYPE_CONVERTED:
                resourceId = getRes(R.plurals.copy_converted_warning_content);
                break;

            case DIALOG_TYPE_FAILURE:
                switch (operationType) {
                    case FileOperationService.OPERATION_COPY:
                        resourceId = getRes(R.plurals.copy_failure_alert_content);
                        break;
                    case FileOperationService.OPERATION_COMPRESS:
                        resourceId = getRes(R.plurals.compress_failure_alert_content);
                        break;
                    case FileOperationService.OPERATION_EXTRACT:
                    case FileOperationService.OPERATION_UNPACK:
                        resourceId = getRes(R.plurals.extract_failure_alert_content);
                        break;
                    case FileOperationService.OPERATION_DELETE:
                        resourceId = getRes(R.plurals.delete_failure_alert_content);
                        break;
                    case FileOperationService.OPERATION_MOVE:
                        resourceId = getRes(R.plurals.move_failure_alert_content);
                        break;
                    default:
                        throw new UnsupportedOperationException();
                }
                break;

            default:
                throw new UnsupportedOperationException();
        }

        final StringBuilder list = new StringBuilder("<p>");
        final BidiFormatter bdf = BidiFormatter.getInstance();

        if (docs != null) {
            for (DocumentInfo doc : docs) {
                list.append("&#8226; ");
                list.append(Html.escapeHtml(bdf.unicodeWrap(doc.displayName)));
                list.append("<br>");
            }
        }

        if (uris != null) {
            for (Uri uri : uris) {
                list.append("&#8226; ");
                list.append(Html.escapeHtml(bdf.unicodeWrap(uri.toSafeString())));
                list.append("<br>");
            }
        }

        if (paths != null) {
            for (String path : paths) {
                list.append("&#8226; ");
                list.append(Html.escapeHtml(bdf.unicodeWrap(new File(path).getName())));
                list.append("<br>");
            }
        }

        list.append("</p>");

        final int totalItems = docs.size() + (uris != null ? uris.size() : 0);
        return mContext.getResources().getQuantityString(resourceId, totalItems, list.toString());
    }

    /**
     * Generates a formatted quantity string.
     */
    public String getQuantityString(@PluralsRes int stringId, int quantity) {
        return Shared.getQuantityString(mContext, stringId, quantity);
    }
}
