/*
 * Copyright (c) 2016 Armel Soro
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.rm3l.maoni.sample.feedback;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.rm3l.maoni.common.contract.Handler;
import org.rm3l.maoni.common.model.Feedback;
import org.rm3l.maoni.email.MaoniEmailListener;
import org.rm3l.maoni.sample.BuildConfig;
import org.rm3l.maoni.sample.R;

/**
 * {@link MaoniEmailListener} is a Maoni listener class allowing to send emails.
 * It comes as an external contrib, which can be included to your {@literal build.gradle},
 * as follows:
 * <p/>
 * <pre>
 *     <code>
 *         dependencies {
 *             //...
 *             compile 'org.rm3l:maoni-email:<versionToReplace>'
 *         }
 *     </code>
 * </pre>
 * <p/>
 * Anyways, you are free to just implement {@link Handler} and provide your own implementation.
 */
public class MyHandlerForMaoni extends MaoniEmailListener implements Handler {

    public static final String EMAIL = "EMAIL";
    private final Context mContext;
    private TextInputLayout mEmailInputLayout;
    private EditText mEmail;
    private EditText mExtraEditText;
    private RadioGroup mExtraRadioGroup;

    public MyHandlerForMaoni(Context context) {
        this(context,
                "text/html",
                "Feedback for Maoni Sample App (" +
                        BuildConfig.APPLICATION_ID + ":" +
                        BuildConfig.VERSION_NAME + ")",
                null,
                null,
                new String[]{"apps+maoni@rm3l.org"},
                null,
                new String[]{"apps+maoni_sample@rm3l.org"});
    }

    private MyHandlerForMaoni(Context context,
                              String mimeType,
                              String subject,
                              String bodyHeader,
                              String bodyFooter,
                              String[] toAddresses,
                              String[] ccAddresses,
                              String[] bccAddresses) {
        super(context,
                mimeType, subject, bodyHeader, bodyFooter,
                toAddresses, ccAddresses, bccAddresses);
        this.mContext = context;
    }

    @Override
    public void onDismiss() {
        Toast.makeText(mContext, "Activity Dismissed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSendButtonClicked(@NonNull Feedback feedback) {
        // Depending on your use case, you may add specific data in the feedback object returned,
        // and manipulate it accordingly
        feedback.put(EMAIL, mEmail.getText());
        feedback.put("EXTRA_EDIT_TEXT", mExtraEditText.getText());
        feedback.put("EXTRA_RADIO_GROUP", mExtraRadioGroup.getCheckedRadioButtonId());
        super.onSendButtonClicked(feedback);
    }

    @Override
    public boolean validateForm(@NonNull View rootView) {
        if (mEmail != null) {
            if (TextUtils.isEmpty(mEmail.getText())) {
                if (mEmailInputLayout != null) {
                    mEmailInputLayout.setErrorEnabled(true);
                    mEmailInputLayout.setError(mContext.getString(R.string.maoni_validate_must_not_be_blank));
                }
                return false;
            } else {
                if (mEmailInputLayout != null) {
                    mEmailInputLayout.setErrorEnabled(false);
                }
            }
        }
        return true;
    }

    @Override
    public void onCreate(@NonNull View rootView, @Nullable final Bundle savedInstanceState) {
        mEmailInputLayout = (TextInputLayout) rootView.findViewById(R.id.extra_email_inputlayout);
        mEmail = (EditText) rootView.findViewById(R.id.extra_email);

        mExtraEditText = (EditText) rootView.findViewById(R.id.extra_edittext);
        mExtraRadioGroup = (RadioGroup) rootView.findViewById(R.id.extra_radiogroup);

        //You may pre-fill some fields accordingly, before they are displayed to the user
        mEmail.setText("a@b.cd", TextView.BufferType.EDITABLE);
    }
}

@@ -2313,56 +2313,8 @@
  %7B%0A%0A
-    public static final String EMAIL = %22EMAIL%22;%0A
     
@@ -3739,24 +3739,25 @@
 feedback) %7B%0A
+%0A
         // D
@@ -3908,13 +3908,15 @@
 put(
-EMAIL
+%22Email%22
 , mE
@@ -3958,23 +3958,26 @@
 ut(%22
-EXTRA_EDIT_TEXT
+My Extra Edit Text
 %22, m
@@ -3994,32 +3994,33 @@
 ext.getText());%0A
+%0A
         feedback
@@ -4016,42 +4016,266 @@
    f
-eedback.put(%22EXTRA_RADIO_GROUP%22,
+inal String myExtraRadioGroupChecked;%0A        switch (mExtraRadioGroup.getCheckedRadioButtonId()) %7B%0A            case R.id.extra_rg1:%0A                myExtraRadioGroupChecked = %22RG 1%22;%0A                break;%0A            case R.id.extra_rg2:%0A               
  m
+y
 Extr
@@ -4289,36 +4289,469 @@
 roup
-.getCheckedRadioButtonId());
+Checked = %22RG 2%22;%0A                break;%0A            case R.id.extra_rg3:%0A                myExtraRadioGroupChecked = %22RG 3%22;%0A                break;%0A            default:%0A                myExtraRadioGroupChecked = null;%0A                break;%0A        %7D%0A        feedback.put(%22My Extra Radio Group%22,%0A                myExtraRadioGroupChecked != null ? myExtraRadioGroupChecked : %22???%22);%0A%0A        //Forward to the Email Listener for opening up the %22Send Email%22 Intent
 %0A   

Update keys for extra fields included in the feedback email