/*
 *    Copyright 2018-2019 adview.com, Inc.
 *
 *    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.adview.mobile.javademo.activities.ads.inapp;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.adview.sdk.AdviewNativeAd;
import com.adview.sdk.NativeAdUnit;
import com.adview.mobile.javademo.activities.BaseAdActivity;
import com.adview.mobile.javademo.utils.ImageUtils;
import com.adview.sdk.rendering.utils.ntv.NativeAdProvider;

import java.util.Arrays;

public class InAppNativeActivity extends BaseAdActivity {

    private static final String CONFIG_ID = "POSIDjdk0claa5fec"; //native official

    private NativeAdUnit nativeAdUnit;
    private int currentTestSizeDp = 320;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        createAd();
    }

    private void createAd() {
        Bundle extras = new Bundle();
        nativeAdUnit = configureNativeAdUnit();

        nativeAdUnit.fetchDemand(extras, resultCode -> {
            if (extras.isEmpty()) {
                return;
            }
            AdviewNativeAd nativeAd = NativeAdProvider.getNativeAd(extras);
            if (nativeAd != null) {
                inflateAdviewNativeAd2(nativeAd);
            }
        });
    }

    private NativeAdUnit configureNativeAdUnit() {
        NativeAdUnit nativeAdUnit = new NativeAdUnit(CONFIG_ID);
        nativeAdUnit.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC);
        nativeAdUnit.setPlacementType(NativeAdUnit.PLACEMENTTYPE.CONTENT_FEED);
        nativeAdUnit.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL);

        return nativeAdUnit;
    }

    private void inflateAdviewNativeAd2(AdviewNativeAd ad) {
        float density = getResources().getDisplayMetrics().density;
        int sizePx = (int) (currentTestSizeDp * density);

        LinearLayout nativeContainer = new LinearLayout(this);
        // Use WRAP_CONTENT to adapt to larger images and create spacing
        FrameLayout.LayoutParams containerLp = new FrameLayout.LayoutParams(sizePx, ViewGroup.LayoutParams.WRAP_CONTENT);
        containerLp.gravity = Gravity.CENTER;
        nativeContainer.setLayoutParams(containerLp);
        nativeContainer.setOrientation(LinearLayout.VERTICAL);
        nativeContainer.setBackgroundColor(Color.WHITE);
        nativeContainer.setPadding(10, 10, 10, 10);

        // 1. Container for icon and text row
        LinearLayout iconAndTitle = new LinearLayout(this);
        iconAndTitle.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams iconAndTitleLp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        iconAndTitleLp.bottomMargin = (int) (10 * density); // 2. Space from the large image below to create a white border effect
        iconAndTitle.setLayoutParams(iconAndTitleLp);

        // 2. Icon slightly smaller (reduced from 0.2 to 0.15)
        ImageView icon = new ImageView(this);
        int iconSize = (int) (sizePx * 0.15);
        icon.setLayoutParams(new LinearLayout.LayoutParams(iconSize, iconSize));
        ImageUtils.download(ad.getIconUrl(), icon);
        iconAndTitle.addView(icon);

        // 1. Text container (Title + Description), located to the right of the Icon
        LinearLayout textContainer = new LinearLayout(this);
        textContainer.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams textContainerLp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
        textContainerLp.leftMargin = (int) (10 * density);
        textContainer.setLayoutParams(textContainerLp);

        // 1. Title: Large, bold, and black
        TextView title = new TextView(this);
        title.setTextSize(currentTestSizeDp < 150 ? 12f : 16f);
        title.setTypeface(null, Typeface.BOLD);
        title.setTextColor(Color.BLACK);
        title.setText(ad.getTitle());
        textContainer.addView(title);

        // 1. Description: Small, placed below the Title
        TextView description = new TextView(this);
        description.setTextSize(currentTestSizeDp < 150 ? 8f : 11f);
        description.setMaxLines(2);
        description.setText(ad.getDescription());
        textContainer.addView(description);

        iconAndTitle.addView(textContainer);
        nativeContainer.addView(iconAndTitle);

        ImageView image = new ImageView(this);
        int imageWidth = sizePx - 20;
        int imageHeight = (int) (sizePx * 0.6); // Increase the height ratio of the large image
        image.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageUtils.download(ad.getImageUrl(), image);
        nativeContainer.addView(image);

        if (ad.getCallToAction() != null && !ad.getCallToAction().isEmpty()) {
            Button ctaButton = new Button(this);
            ctaButton.setText(ad.getCallToAction());
            // 3. Blue background with white text
            ctaButton.setBackgroundColor(Color.parseColor("#2196F3"));
            ctaButton.setTextColor(Color.WHITE);
            LinearLayout.LayoutParams ctaLp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    (int) (sizePx * 0.15)
            );
            // Add spacing/white border between it and the large image
            ctaLp.topMargin = (int) (10 * density);
            ctaButton.setLayoutParams(ctaLp);
            nativeContainer.addView(ctaButton);
        }

        getAdWrapperView().addView(nativeContainer);

        ad.registerView(
                nativeContainer,
                Arrays.asList(icon, title, image, description),
                null
        );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (nativeAdUnit != null) {
            nativeAdUnit.destroy();
        }
    }
}
