package org.mercury_im.messenger.android.ui.contacts.detail;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import org.mercury_im.messenger.android.MercuryImApplication;
import org.mercury_im.messenger.R;
import org.mercury_im.messenger.android.di.module.AndroidSchedulersModule;
import org.mercury_im.messenger.android.ui.base.MercuryActivity;
import org.mercury_im.messenger.android.util.ArgumentUtils;
import org.mercury_im.messenger.core.data.repository.PeerRepository;

import java.util.UUID;

import javax.inject.Inject;

import butterknife.ButterKnife;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import lombok.Value;

public class ContactDetailActivity extends AppCompatActivity implements MercuryActivity {
    public static final String EXTRA_PEER_ID = "PEER_ID";

    private AndroidContactDetailViewModel androidContactDetailViewModel;
    private UUID peerId;

    Disposable disposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MercuryImApplication.getApplication().getAppComponent().inject(this);

        Arguments arguments = getArguments(savedInstanceState);
        peerId = arguments.getPeerId();

        androidContactDetailViewModel = new ViewModelProvider(this).get(AndroidContactDetailViewModel.class);
        bindUiComponents();

        disposable = androidContactDetailViewModel.init(peerId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }

    private void bindUiComponents() {
        setContentView(R.layout.layout_top_toolbar);
        ButterKnife.bind(this);

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment, new ContactDetailFragment(), "contact_details")
                .commit();
    }

    private Arguments getArguments(Bundle bundle) {
        bundle = requiredExtras(bundle, getIntent());
        UUID peerId = ArgumentUtils.requireUUID(bundle, EXTRA_PEER_ID);
        return new Arguments(peerId);
    }

    @Value
    private class Arguments {
        UUID peerId;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.dispose();
    }
}
