/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
package com.facebook.react.views.checkbox;

import android.widget.CompoundButton;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.EventDispatcher;

/** View manager for {@link ReactCheckBox} components. */
public class ReactCheckBoxManager extends SimpleViewManager<ReactCheckBox> {

  private static final String REACT_CLASS = "AndroidCheckBox";

  private static final CompoundButton.OnCheckedChangeListener ON_CHECKED_CHANGE_LISTENER =
      new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          ReactContext reactContext = (ReactContext) buttonView.getContext();
          reactContext
              .getNativeModule(UIManagerModule.class).getEventDispatcher()
              .dispatchEvent(new ReactCheckBoxEvent(buttonView.getId(), isChecked));
        }
      };

  @Override
  public String getName() {
    return REACT_CLASS;
  }

  @Override
  protected void addEventEmitters(final ThemedReactContext reactContext, final ReactCheckBox view) {
    view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
  }

  @Override
  protected ReactCheckBox createViewInstance(ThemedReactContext context) {
    ReactCheckBox view = new ReactCheckBox(context);
    return view;
  }

  @ReactProp(name = ViewProps.ENABLED, defaultBoolean = true)
  public void setEnabled(ReactCheckBox view, boolean enabled) {
    view.setEnabled(enabled);
  }

  @ReactProp(name = ViewProps.ON)
  public void setOn(ReactCheckBox view, boolean on) {
    // we set the checked change listener to null and then restore it so that we don't fire an
    // onChange event to JS when JS itself is updating the value of the checkbox
    view.setOnCheckedChangeListener(null);
    view.setOn(on);
    view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
  }
}

@@ -226,16 +226,101 @@
 ckbox;%0A%0A
+import android.content.Context;%0Aimport android.support.v7.widget.TintContextWrapper;%0A
 import a
@@ -349,16 +349,16 @@
 Button;%0A
-
 import c
@@ -669,68 +669,8 @@
 rop;
-%0Aimport com.facebook.react.uimanager.events.EventDispatcher;
 %0A%0A/*
@@ -1152,17 +1152,19 @@
 ntext = 
-(
+get
 ReactCon
@@ -1167,18 +1167,17 @@
 tContext
-) 
+(
 buttonVi
@@ -1182,20 +1182,8 @@
 View
-.getContext(
 );%0A 
@@ -1279,16 +1279,16 @@
 tcher()%0A
-
         
@@ -1373,16 +1373,454 @@
        %7D
+%0A%0A        private ReactContext getReactContext(CompoundButton buttonView) %7B%0A          ReactContext reactContext;%0A          Context ctx = buttonView.getContext();%0A          if (ctx instanceof TintContextWrapper) %7B%0A            reactContext = (ReactContext) ((TintContextWrapper) ctx).getBaseContext();%0A          %7D else %7B%0A            reactContext = (ReactContext) buttonView.getContext();%0A          %7D%0A          return reactContext;%0A        %7D
 %0A      %7D

fix checkbox casting crash in old android versions (#23281)