/*
 * Copyright (c) 2016 Qiscus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qiscus.sdk.ui.adapter.viewholder;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.R;
import com.qiscus.sdk.data.model.QiscusComment;
import com.qiscus.sdk.ui.adapter.OnItemClickListener;
import com.qiscus.sdk.ui.adapter.OnLongItemClickListener;
import com.qiscus.sdk.util.QiscusRawDataExtractor;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created on : June 09, 2017
 * Author     : zetbaitsu
 * Name       : Zetra
 * GitHub     : https://github.com/zetbaitsu
 */
public abstract class QiscusBaseCardMessageViewHolder extends QiscusBaseTextMessageViewHolder {

    @NonNull protected View cardView;
    @Nullable protected ImageView imageView;
    @Nullable protected TextView titleView;
    @Nullable protected TextView descriptionView;

    protected int titleTextColor;
    protected int descriptionTextColor;

    public QiscusBaseCardMessageViewHolder(View itemView, OnItemClickListener itemClickListener,
                                           OnLongItemClickListener longItemClickListener) {
        super(itemView, itemClickListener, longItemClickListener);
        cardView = getCardView(itemView);
        imageView = getImageView(itemView);
        titleView = getTitleView(itemView);
        descriptionView = getDescriptionView(itemView);
    }

    @NonNull
    protected abstract View getCardView(View itemView);

    @Nullable
    protected abstract ImageView getImageView(View itemView);

    @Nullable
    protected abstract TextView getTitleView(View itemView);

    @Nullable
    protected abstract TextView getDescriptionView(View itemView);

    @Override
    protected void loadChatConfig() {
        super.loadChatConfig();
        titleTextColor = ContextCompat.getColor(Qiscus.getApps(), Qiscus.getChatConfig().getCardTitleColor());
        descriptionTextColor = ContextCompat.getColor(Qiscus.getApps(), Qiscus.getChatConfig().getCardDescriptionColor());
    }

    @Override
    protected void setUpColor() {
        super.setUpColor();
        if (titleView != null) {
            titleView.setTextColor(titleTextColor);
        }
        if (descriptionView != null) {
            descriptionView.setTextColor(descriptionTextColor);
        }
    }

    @Override
    protected void showMessage(QiscusComment qiscusComment) {
        super.showMessage(qiscusComment);
        Log.d("ZETRA", "Holaaaa");
        try {
            JSONObject payload = QiscusRawDataExtractor.getPayload(qiscusComment);
            setUpCard(payload);
        } catch (JSONException | NullPointerException e) {
            e.printStackTrace();
            cardView.setVisibility(View.GONE);
        }
    }

    private void setUpCard(JSONObject payload) {
        cardView.setOnClickListener(v -> new CustomTabsIntent.Builder()
                .setToolbarColor(ContextCompat.getColor(Qiscus.getApps(), Qiscus.getChatConfig().getAppBarColor()))
                .setShowTitle(true)
                .addDefaultShareMenuItem()
                .enableUrlBarHiding()
                .build()
                .launchUrl(cardView.getContext(), Uri.parse(payload.optString("url"))));

        if (imageView != null) {
            Glide.with(imageView.getContext())
                    .load(payload.optString("image", ""))
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.drawable.qiscus_image_placeholder)
                    .error(R.drawable.qiscus_image_placeholder)
                    .into(imageView);
        }
        if (titleView != null) {
            titleView.setText(payload.optString("title", ""));
        }
        if (descriptionView != null) {
            descriptionView.setText(payload.optString("description", ""));
        }
    }
}

@@ -851,33 +851,8 @@
 at;%0A
-import android.util.Log;%0A
 impo
@@ -3329,43 +3329,8 @@
 t);%0A
-        Log.d(%22ZETRA%22, %22Holaaaa%22);%0A
     

Clean up