package com.plugin.contact;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;


@CapacitorPlugin(name = "Plugin")
public class ContactPlugin extends Plugin{
    private Contact implementation = new Contact();

    @PluginMethod
    public void echo(PluginCall call) {
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", implementation.echo(value));
        call.resolve(ret);
    }

    @PluginMethod
    public void addContact(PluginCall call) {
        String name = call.getString("name");
        String mobile = call.getString("phone");
        String phone = call.getString("ext");
        String department = call.getString("dept");
        String email = call.getString("email");
        if(implementation == null) {
            implementation = new Contact();
        }
        implementation.save(getActivity(), name, email, mobile, phone, department, new Contact.OnResult() {
            @Override
            public void onSuccess() {
                JSObject ret = new JSObject();
                ret.put("result", true);
                ret.put("message", "완료");
                call.resolve(ret);
            }

            @Override
            public void onFail() {
                JSObject ret = new JSObject();
                ret.put("result", false);
                ret.put("message", "존재하는 번호입니다.");
                call.resolve(ret);
            }
        });
    }
}
