package com.pointzi.react;
import android.os.Handler;
import android.view.View;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import com.pointzi.core.StreetHawk;
import com.facebook.react.bridge.ReactMethod;
import com.pointzi.Player;
import com.pointzi.Pointzi;

public class PointziReact extends ReactContextBaseJavaModule implements ReactFindViewUtil.OnViewFoundListener {

    public PointziReact(ReactApplicationContext reactContext) {
        super(reactContext);
        new ReactFindViewUtil().addViewListener(this);
    }

    @Override
    public String getName() {
        return "PointziReact";
    }

    @ReactMethod
    public void tagCuid(String tag){
        StreetHawk.INSTANCE.tagString("sh_cuid", tag);
    }

    @ReactMethod
    public void tagString(String key, String tag){
        StreetHawk.INSTANCE.tagString(key, tag);
    }

    @ReactMethod
    public void tagNumeric(String key, double numericValue){
        Pointzi.INSTANCE.tagNumeric(key, numericValue);
    }

    /*
      Date format in YYYY-MM-DD
      https://streethawk.freshdesk.com/solution/articles/5000672617-analytics
    */
    @ReactMethod
    public void tagDatetime(String key, String date){
        Pointzi.INSTANCE.tagDatetime(key, date);
    }

    @ReactMethod
    public void incrementTag(String key){
        Pointzi.INSTANCE.incrementTag(key);
    }

    @ReactMethod
    public void incrementTag(String key, int value){
        Pointzi.INSTANCE.incrementTag(key, value);
    }

    @ReactMethod
    public void removeTag(String key){
        Pointzi.INSTANCE.removeTag(key);
    }

    @ReactMethod
    public void tagUserLanguage(String language){
        StreetHawk.Tagger.INSTANCE.tagUserLanguage(language);
    }
    
    @ReactMethod
    public void showGuide(String id){
        Pointzi.showOnDemandCampaign(id);
    }
    
    /**
     * Deprecated method please use viewWillRender()
     * This method only works for Pointzi SDK 1.4.21 and above.
     * Sends ViewName to Pointzi for Tips to target.
     * @param reactViewName name of View
     */  
   @ReactMethod
   public void pointziReactWillDidMount(String reactViewName){
	 new Player().setReactViewActivity(reactViewName, getCurrentActivity());
   }
   
    /**
     * This method only works for Pointzi SDK 1.4.21 and above.
     * Sends ViewName to Pointzi for Tips to target.
     * @param reactViewName name of View
     */  
     @ReactMethod
     public void viewWillRender(String reactViewName){
        new Player().setReactViewActivity(reactViewName, getCurrentActivity());
     }

    @Override
    public String getNativeId() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                new Player().onEnteringNewActivity(getCurrentActivity());
            }
        }, 500);
        return null;
    }

    @Override
    public void onViewFound(View view) {
      
    }
}