package com.pokkt.plugin.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 com.pokkt.PokktAds;
import com.pokkt.plugin.papreact.PokktNativeExtension;
import com.pokkt.sdk.banners.PokktBannerView;

import org.json.JSONObject;

class PokktBannerAdapter {
    private static final int BANNER_WIDTH = 320;
    private static final int BANNER_HEIGHT = 50;
    static PokktBannerView bannerContainer = null;
    static PokktAds.BannerAdDelegate bannerAdDelegate = new PokktAds.BannerAdDelegate() {
        public void bannerExpanded(String screenId) {
            try {
                PokktAds.Debugging.log("[POKKT-JAVA]: bannerExpanded: " + screenId);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("SCREEN_ID", screenId);
                PokktNativeExtension.notifyFramework(PokktToFrameworkEvents.EVENT_BANNER_EXPANDED, jsonObject.toString());
            } catch (Throwable e) {
                PokktAds.Debugging.printStackTrace("",e);
            }
        }

        public void bannerResized(String screenId) {
            try {
                PokktAds.Debugging.log("[POKKT-JAVA]: bannerResized: " + screenId);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("SCREEN_ID", screenId);
                PokktNativeExtension.notifyFramework(PokktToFrameworkEvents.EVENT_BANNER_RESIZED, jsonObject.toString());
            } catch (Throwable e) {
                PokktAds.Debugging.printStackTrace("",e);
            }
        }

        public void bannerCollapsed(String screenId) {
            try {
                PokktAds.Debugging.log("[POKKT-JAVA]: bannerCollapsed: " + screenId);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("SCREEN_ID", screenId);
                PokktNativeExtension.notifyFramework(PokktToFrameworkEvents.EVENT_BANNER_COLLAPSED, jsonObject.toString());
            } catch (Throwable e) {
                PokktAds.Debugging.printStackTrace("",e);
            }
        }

        @Override
        public void adCachingResult(String screenId, boolean isSuccess, double reward, String errorMessage) {

        }

        @Override
        public void adDisplayedResult(String screenId, boolean isSuccess, String errorMessage) {
            try {
                PokktAds.Debugging.log("[POKKT-JAVA]: adDisplayedResult: " + screenId + " " + errorMessage);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("SCREEN_ID", screenId);
                jsonObject.put("IS_SUCCESS", isSuccess);
                jsonObject.put("ERROR_MESSAGE", errorMessage);
                PokktNativeExtension.notifyFramework(PokktToFrameworkEvents.EVENT_AD_DISPLAYED_RESULT, jsonObject.toString());
            } catch (Throwable e) {
                PokktAds.Debugging.printStackTrace("",e);
            }
        }

        @Override
        public void adClosed(String screenId, boolean b) {

        }

        @Override
        public void adGratified(String screenId, double v) {

        }

        public void adClicked(String screenId) {
            try {
                PokktAds.Debugging.log("[POKKT-JAVA]: bannerClicked: " + screenId);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("SCREEN_ID", screenId);
                PokktNativeExtension.notifyFramework(PokktToFrameworkEvents.EVENT_AD_CLICKED, jsonObject.toString());
            } catch (Throwable e) {
                PokktAds.Debugging.printStackTrace("",e);
            }
        }
    };

    static void destroyBanner(Activity activity) {
        PokktAdsAdapter.runOnUiHelper(new PokktAdsAdapter.IUIHelperFunction() {
            public void execute() {
                try {
                    if (PokktBannerAdapter.bannerContainer != null) {
                        PokktAds.destroyBanner(PokktBannerAdapter.bannerContainer);
                    }
                } catch (Exception e) {
                    PokktAds.Debugging.printStackTrace("",e);
                }
            }
        }, activity);
    }

    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:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.TOP_LEFT");
                rlParams.gravity = Gravity.START|Gravity.TOP;
                break;
            case BannerPosition.TOP_CENTER:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.TOP_CENTER");
                rlParams.gravity = Gravity.CENTER|Gravity.TOP;
                break;
            case BannerPosition.TOP_RIGHT:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.TOP_RIGHT");
                rlParams.gravity = Gravity.END|Gravity.TOP;
                break;
            case BannerPosition.MIDDLE_LEFT:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.MIDDLE_LEFT");
                rlParams.gravity = Gravity.START|Gravity.CENTER_VERTICAL;
                break;
            case BannerPosition.MIDDLE_CENTER:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.MIDDLE_CENTER");
                rlParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
                break;
            case BannerPosition.MIDDLE_RIGHT:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.MIDDLE_RIGHT");
                rlParams.gravity = Gravity.END|Gravity.CENTER_VERTICAL;
                break;
            case BannerPosition.BOTTOM_LEFT:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.BOTTOM_LEFT");
                rlParams.gravity = Gravity.START|Gravity.BOTTOM;
                break;
            case BannerPosition.BOTTOM_CENTER:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.BOTTOM_CENTER");
                rlParams.gravity = Gravity.CENTER|Gravity.BOTTOM;
                break;
            case BannerPosition.BOTTOM_RIGHT:
                PokktAds.Debugging.log("[POKKT-JAVA]: BannerPosition.BOTTOM_RIGHT");
                rlParams.gravity = Gravity.END|Gravity.BOTTOM;
                break;
            default: //TOP_CENTER
                PokktAds.Debugging.log("[POKKT-JAVA]: Did not find any matching request for banner position so setting to TOP_CENTER");
                rlParams.gravity = Gravity.START|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;
    }
}