package com.pokkt.papreact.common;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.pokkt.PokktAds;
import com.pokkt.sdk.banners.PokktBannerView;
import com.pokkt.sdk.debugging.Logger;

import org.json.JSONObject;

class PokktBannerAdapter {

    private static final int BANNER_WIDTH = 320;
    private static final int BANNER_HEIGHT = 50;

    private static PokktBannerView bannerContainer = null;

    public static void loadBanner(final String jsonString, final Activity activity) {
        PokktAdsAdapter.runOnUiHelper(new PokktAdsAdapter.IUIHelperFunction() {
            @Override
            public void execute() {
                try {
                    Logger.d("loadBanner method called");
                    JSONObject jsonObject = new JSONObject(jsonString);
                    String screenName = jsonObject.optString("screenName");
                    int bannerTypes = Integer.parseInt(jsonObject.optString("bannerPosition","1"));

                    // remove existing banner
                    if (bannerContainer != null) {
                        PokktAds.Banner.destroy(bannerContainer);
                        bannerContainer = null;
                    }

                    bannerContainer = new PokktBannerView(activity);
                    activity.addContentView(bannerContainer, getLayoutParams(bannerTypes, activity));
                    PokktAds.Banner.loadBanner(screenName, bannerContainer);
                } catch (Exception e) {
                    Logger.printStackTrace(e);
                }
            }
        }, activity);
    }

    public static void loadBannerWithRect(final String jsonString, final Activity activity) {
        PokktAdsAdapter.runOnUiHelper(new PokktAdsAdapter.IUIHelperFunction() {
            @Override
            public void execute() {
                try {
                    Logger.d("loadBannerRect method called");

                    JSONObject jsonObject = new JSONObject(jsonString);
                    String screenName = jsonObject.optString("screenName");

                    // setting this position when if developer is giving some value for x or y.
                    int width = Integer.parseInt(jsonObject.optString("width"));
                    int height = Integer.parseInt(jsonObject.optString("height"));

                    // remove existing banner
                    if (bannerContainer != null) {
                        PokktAds.Banner.destroy(bannerContainer);
                        bannerContainer = null;
                    }

                    bannerContainer = new PokktBannerView(activity);
                    bannerContainer.setX(Float.parseFloat(jsonObject.optString("x")));
                    bannerContainer.setY(Float.parseFloat(jsonObject.optString("y")));

                    activity.addContentView(bannerContainer, new RelativeLayout.LayoutParams(width, height));
                    PokktAds.Banner.loadBanner(screenName, bannerContainer);
                } catch (Exception e) {
                    Logger.printStackTrace(e);
                }
            }
        }, activity);
    }

    public static void destroyBanner(final Activity activity) {
        PokktAdsAdapter.runOnUiHelper(new PokktAdsAdapter.IUIHelperFunction() {
            @Override
            public void execute() {
                try {
                    if (bannerContainer != null)
                        PokktAds.Banner.destroy(bannerContainer);
                } catch (Exception e) {
                    Logger.printStackTrace(e);
                }
            }
        }, activity);
    }

    private static FrameLayout.LayoutParams getLayoutParams(int position, Activity activity) {
        FrameLayout.LayoutParams rlParams = new FrameLayout.LayoutParams(
                dpToPx(activity, BANNER_WIDTH), dpToPx(activity, BANNER_HEIGHT));

        switch (position) {
            case BannerPosition.TOP_LEFT:
                Logger.d("[POKKT-JAVA]: BannerPosition.TOP_LEFT");
                rlParams.gravity = Gravity.LEFT|Gravity.TOP;
                break;
            case BannerPosition.TOP_CENTER:
                Logger.d("[POKKT-JAVA]: BannerPosition.TOP_CENTER");
                rlParams.gravity = Gravity.CENTER|Gravity.TOP;
                break;
            case BannerPosition.TOP_RIGHT:
                Logger.d("[POKKT-JAVA]: BannerPosition.TOP_RIGHT");
                rlParams.gravity = Gravity.RIGHT|Gravity.TOP;
                break;
            case BannerPosition.MIDDLE_LEFT:
                Logger.d("[POKKT-JAVA]: BannerPosition.MIDDLE_LEFT");
                rlParams.gravity = Gravity.LEFT|Gravity.CENTER_VERTICAL;
                break;
            case BannerPosition.MIDDLE_CENTER:
                Logger.d("[POKKT-JAVA]: BannerPosition.MIDDLE_CENTER");
                rlParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
                break;
            case BannerPosition.MIDDLE_RIGHT:
                Logger.d("[POKKT-JAVA]: BannerPosition.MIDDLE_RIGHT");
                rlParams.gravity = Gravity.RIGHT|Gravity.CENTER_VERTICAL;
                break;
            case BannerPosition.BOTTOM_LEFT:
                Logger.d("[POKKT-JAVA]: BannerPosition.BOTTOM_LEFT");
                rlParams.gravity = Gravity.LEFT|Gravity.BOTTOM;
                break;
            case BannerPosition.BOTTOM_CENTER:
                Logger.d("[POKKT-JAVA]: BannerPosition.BOTTOM_CENTER");
                rlParams.gravity = Gravity.CENTER|Gravity.BOTTOM;
                break;
            case BannerPosition.BOTTOM_RIGHT:
                Logger.d("[POKKT-JAVA]: BannerPosition.BOTTOM_RIGHT");
                rlParams.gravity = Gravity.RIGHT|Gravity.BOTTOM;
                break;
            default: //TOP_CENTER
                Logger.d("[POKKT-JAVA]: Did not find any matching request for banner position so setting to TOP_CENTER");
                rlParams.gravity = Gravity.LEFT|Gravity.TOP;
        }
        return rlParams;
    }

    private static int dpToPx(DisplayMetrics metrics, int dp) {
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
        if (px < 1.0f) {
            px = 1;
        }
        return (int) px;
    }

    private static int dpToPx(Context context, int dp) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        return dpToPx(metrics, dp);
    }

    static class BannerPosition {
        static final int TOP_LEFT = 1;
        static final int TOP_CENTER = 2;
        static final int TOP_RIGHT = 3;
        static final int MIDDLE_LEFT = 4;
        static final int MIDDLE_CENTER = 5;
        static final int MIDDLE_RIGHT = 6;
        static final int BOTTOM_LEFT = 7;
        static final int BOTTOM_CENTER = 8;
        static final int BOTTOM_RIGHT = 9;
    }
}
