package org.keyscience.fragments;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.keyscience.R;
import org.keyscience.app.Constants;

public class ContactUsFragment extends Fragment implements View.OnClickListener {

    private TextView phone;
    private TextView orgEmail;
    private Button sendContactInfo;

    private EditText name;
    private EditText email;
    private EditText subject;
    private EditText message;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contactus_fragment, container, false);
        phone = view.findViewById(R.id.phone);
        orgEmail = view.findViewById(R.id.org_email);
        sendContactInfo = view.findViewById(R.id.sendButton);

        name = view.findViewById(R.id.name);
        email = view.findViewById(R.id.email);
        subject = view.findViewById(R.id.subject);
        message = view.findViewById(R.id.message);

        phone.setOnClickListener(this);
        sendContactInfo.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            // dial the org's phone number
            case R.id.phone:
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse(Constants.PHONE_NUMBER));
                if (callIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(callIntent);
                }
                break;
            // send email to org
            case R.id.org_email:
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto", Constants.ORG_EMAIL, null));
                if (emailIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(emailIntent);
                }
                break;
            case R.id.sendButton:
                // check that name field is not empty
                if (!name.getText().toString().equals("") && !email.getText().toString().equals("")) {

//                    HashMap<String, String> values = new HashMap<>();
//                    values.put(Constants.EMAIL, email.getText().toString());
//                    values.put(Constants.NAME, name.getText().toString());
//                    values.put(Constants.MESSAGE, message.getText().toString());
//                    values.put(Constants.SUBJECT, subject.getText().toString());
//
//                    new SendContactAsyncTask().execute(values);

                    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                            "mailto", Constants.ORG_EMAIL, null));
                    intent.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
                    String body = "Name: " + name.getText().toString() + ".\n"
                            + "Email: " + email.getText().toString() + "\n"
                            + "\n\n" + message.getText().toString();

                    intent.putExtra(Intent.EXTRA_TEXT, body);

                    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                        startActivity(intent);
                        name.setText("");
                        email.setText("");
                        subject.setText("");
                        message.setText("");
                    }
                    break;
                } else {
                    Toast.makeText(getContext(), "Required Fields Are Empty", Toast.LENGTH_SHORT).show();
                }


                break;
        }
    }
}
