/*
 * Copyright 2025 Circle Internet Group, Inc. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 * 
 * 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.cybavo.reactnative.wallet.service.view;

import android.content.Context;
import android.view.View;

import com.cybavo.wallet.service.view.NumericPinCodeInputView;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup;
import com.facebook.react.views.view.MeasureUtil;
import com.facebook.yoga.YogaMeasureFunction;
import com.facebook.yoga.YogaMeasureMode;
import com.facebook.yoga.YogaMeasureOutput;
import com.facebook.yoga.YogaNode;

public class NumericPinCodeInputViewShadowNode extends LayoutShadowNode implements YogaMeasureFunction {

    private NumericPinCodeInputView mShadowView;

    public NumericPinCodeInputViewShadowNode() {
        setMeasureFunction(this);
    }

    @Override
    public long measure(YogaNode node, float width, YogaMeasureMode widthMode, float height, YogaMeasureMode heightMode) {
        // it's kind of stupid but I don't know how to map Views with ShadowNodes, so we create a shadow view just for measuring
        View view = getView();
        if (view == null) {
            return YogaMeasureOutput.make(0, 0);
        }

        view.measure(
                MeasureUtil.getMeasureSpec(width, widthMode),
                MeasureUtil.getMeasureSpec(height, heightMode));
        return YogaMeasureOutput.make(view.getMeasuredWidth(), view.getMeasuredHeight());
    }

    private NumericPinCodeInputView getView() {
        if (mShadowView == null) {
            Context ctx = getThemedContext();
            if (ctx == null) {
                return null;
            }
            mShadowView = new NumericPinCodeInputView(getThemedContext());
        }
        return mShadowView;
    }

    // bridge properties
    @ReactProp(name = "horizontalSpacing")
    public void setHorizontalSpacing(int spacing) {
        NumericPinCodeInputView view = getView();
        if (view != null) {
            view.setHorizontalSpacing((int) PixelUtil.toPixelFromDIP(spacing));
            dirty();
        }
    }
    @ReactProp(name = "backspaceButtonText")
    public void setBackspaceButtonText(String text) {
        NumericPinCodeInputView view = getView();
        if (view != null) {
            view.setBackspaceButtonText(text);
            dirty();
        }
    }

    @ReactProp(name = "verticalSpacing")
    public void setVerticalSpacing(int spacing) {
        NumericPinCodeInputView view = getView();
        if (view != null) {
            view.setVerticalSpacing((int) PixelUtil.toPixelFromDIP(spacing));
            dirty();
        }
    }

    @ReactPropGroup(
            names = {
                    "buttonWidth",
                    "backspaceButtonWidth",
            },
            defaultFloat = 0
    )
    public void setButtonWidth(int index, float width) {
        NumericPinCodeInputView view = getView();
        if (view != null) {
            ButtonPropertyHolder.setWidth(view, (int) PixelUtil.toPixelFromDIP(width));
            dirty();
        }
    }

    @ReactPropGroup(
            names = {
                    "buttonHeight",
                    "backspaceButtonHeight",
            },
            defaultFloat = 0
    )
    public void setButtonHeight(int index, float height) {
        NumericPinCodeInputView view = getView();
        if (view != null) {
            ButtonPropertyHolder.setHeight(view, (int) PixelUtil.toPixelFromDIP(height));
            dirty();
        }
    }

}
