package ch.threema.app.adapters.decorators;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import ch.threema.app.R;
import ch.threema.app.ui.listitemholder.ComposeMessageHolder;
import ch.threema.app.utils.GeoLocationUtil;
import ch.threema.app.utils.LinkifyUtil;
import ch.threema.app.utils.RuntimeUtil;
import ch.threema.storage.models.AbstractMessageModel;
import ch.threema.storage.models.MessageState;
import ch.threema.storage.models.data.LocationDataModel;

import static android.view.View.GONE;

public class LocationChatAdapterDecorator extends ChatAdapterDecorator {

    public LocationChatAdapterDecorator(
        AbstractMessageModel messageModel,
        @NonNull ChatAdapterDecoratorListener chatAdapterDecoratorListener,
        @NonNull LinkifyUtil.LinkifyListener linkifyListener,
        Helper helper
    ) {
        super(messageModel, chatAdapterDecoratorListener, linkifyListener, helper);
    }

    @SuppressLint("StaticFieldLeak")
    @Override
    protected void configureChatMessage(@NonNull final ComposeMessageHolder holder, Context context, final int position) {
        final LocationDataModel locationDataModel = this.getMessageModel().getLocationData();

        TextView addressLine = holder.bodyTextView;

        this.setOnClickListener(v -> {
            if (
                getMessageModel().getState() != MessageState.FS_KEY_MISMATCH &&
                    getMessageModel().getState() != MessageState.SENDFAILED
            ) {
                if (!isInChoiceMode()) {
                    viewLocation(v.getContext(), getMessageModel());
                }
            }
        }, holder.messageBlockView);

        //clear texts
        if (holder.bodyTextView != null) {
            holder.bodyTextView.setText("");
        }

        if (holder.secondaryTextView != null) {
            holder.secondaryTextView.setText("");
            holder.secondaryTextView.setVisibility(GONE);
        }

        if (locationDataModel.poiNameOrNull != null) {
            if (holder.bodyTextView != null) {
                holder.bodyTextView.setText(highlightMatches(context, locationDataModel.poiNameOrNull, filterString));
                holder.bodyTextView.setWidth(this.getThumbnailWidth());
                holder.bodyTextView.setVisibility(View.VISIBLE);
            }
            addressLine = holder.secondaryTextView;
        }

        if (addressLine != null) {
            final @Nullable String poiAddress = locationDataModel.poiAddressOrNull;
            if (poiAddress != null) {
                addressLine.setText(highlightMatches(context, poiAddress, filterString));
                addressLine.setWidth(this.getThumbnailWidth());
                addressLine.setVisibility(View.VISIBLE);
            } else {
                GeoLocationUtil geoLocation = new GeoLocationUtil(addressLine);
                Location location = new Location("X");
                location.setLatitude(locationDataModel.latitude);
                location.setLongitude(locationDataModel.longitude);
                location.setAccuracy((long) locationDataModel.accuracyOrFallback);
                geoLocation.updateAddressAndModel(context, location);
            }
        }

        if (position == holder.position) {
            holder.controller.setBackgroundImage(null);
            holder.controller.setIconResource(R.drawable.ic_location_pin);
        }

        RuntimeUtil.runOnUiThread(() -> setupResendStatus(holder));
    }

    private void viewLocation(Context context, AbstractMessageModel messageModel) {
        if (messageModel == null) {
            return;
        }

        if (!GeoLocationUtil.viewLocation(context, messageModel.getLocationData())) {
            RuntimeUtil.runOnUiThread(() -> Toast.makeText(context, "Feature not available due to firmware error", Toast.LENGTH_LONG).show());
        }
    }
}
