package com.rnoneclick.config;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobile.auth.gatewayauth.AuthRegisterViewConfig;
import com.mobile.auth.gatewayauth.AuthRegisterXmlConfig;
import com.mobile.auth.gatewayauth.AuthUIConfig;
import com.mobile.auth.gatewayauth.CustomInterface;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ui.AbstractPnsViewDelegate;
import com.rnoneclick.R;

public class DialogBottomConfig extends BaseUIConfig {

    public static int dp2px(Context context, float dipValue) {
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dipValue * scale + 0.5f);
        } catch (Exception e) {
            return (int) dipValue;
        }
    }

    public DialogBottomConfig(Activity activity, PhoneNumberAuthHelper authHelper) {
        super(activity, authHelper);
    }

    protected View initView(int marginTop) {

        TextView switchTV = new TextView(mContext);
        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dp2px(mContext, 100));
        params.setMargins(0, dp2px(mContext, marginTop), 0, 0);
        relativeLayout.setLayoutParams(params);


        RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        //一键登录按钮默认marginTop 270dp
        mLayoutParams.setMargins(0, dp2px(mContext, 14), 0, 0);
        mLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        switchTV.setText("");
        switchTV.setTextColor(Color.parseColor("#FF666666"));
        switchTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12.0F);
        switchTV.setLayoutParams(mLayoutParams);
        relativeLayout.addView(switchTV);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.layout_protect, null);
        RelativeLayout.LayoutParams inflateParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        inflateParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        inflateParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        inflate.setLayoutParams(inflateParams);
        relativeLayout.addView(inflate);

        relativeLayout.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                RelativeLayout relView = (RelativeLayout) v.getParent().getParent();
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relView.getLayoutParams();
                layoutParams.setMargins(0, dp2px(mContext, 37), 0, 0);
                relView.setLayoutParams(layoutParams);
//                 ((RelativeLayout) relView.findViewById(R.id.authsdk_login_view)).setBackgroundResource(R.drawable.btn_shape);

            }

            @Override
            public void onViewDetachedFromWindow(View v) {

            }
        });


        return relativeLayout;
    }

    @Override
    public void configAuthPage() {
        mAuthHelper.removeAuthRegisterXmlConfig();
        mAuthHelper.removeAuthRegisterViewConfig();
        int authPageOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
        if (Build.VERSION.SDK_INT == 26) {
            authPageOrientation = ActivityInfo.SCREEN_ORIENTATION_BEHIND;
        }
        updateScreenSize(authPageOrientation);
        int dialogHeight = 280;
        //sdk默认控件的区域是marginTop50dp
        int designHeight = 280;
        int unit = designHeight / 10;
        int logBtnHeight = (int) (unit * 1.2);
        mAuthHelper.addAuthRegistViewConfig("switch_msg", new AuthRegisterViewConfig.Builder()
                .setView(initView(100))
                .setRootViewId(AuthRegisterViewConfig.RootViewId.ROOT_VIEW_ID_BODY)
                .setCustomInterface(new CustomInterface() {
                    @Override
                    public void onClick(Context context) {
                        if (getOtherLogin() != null) {
                            getOtherLogin().call(1);
                        }
                        mAuthHelper.quitLoginPage();
                    }
                })
                .build());
        mAuthHelper.addAuthRegisterXmlConfig(new AuthRegisterXmlConfig.Builder()
                .setLayout(R.layout.custom_port_dialog_action_bar, new AbstractPnsViewDelegate() {
                    @Override
                    public void onViewCreated(View view) {

                        findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mAuthHelper.quitLoginPage();
                            }
                        });
                    }
                })
                .build());

        int color = Color.parseColor("#FD5A27");
        if (!TextUtils.isEmpty(OneKeyConfig.MAIN_COLOR)) {
            color = Color.parseColor(OneKeyConfig.MAIN_COLOR);
        }
        mAuthHelper.setAuthUIConfig(new AuthUIConfig.Builder()
                .setAppPrivacyOne("《注册协议》", OneKeyConfig.REGISTER_PRIVACY_URL)
                .setAppPrivacyTwo(" 《隐私保护协议》", OneKeyConfig.PROTECT_PRIVACY_URL)
                .setAppPrivacyColor(Color.parseColor("#999999"), color)
                .setWebViewStatusBarColor(Color.TRANSPARENT)
                .setNavHidden(true)
                .setPrivacyState(false)
                .setCheckboxHidden(true)

                .setLogoImgPath("ic_one_key_login_logo")
                .setLogoOffsetY(0)
                .setLogoWidth(55)
                .setLogoHeight(55)
                .setLogBtnText(OneKeyConfig.LOGIN_BTN_TEXT)

                .setNumFieldOffsetY(84)
                .setNumberSize(18)

                .setSloganText(OneKeyConfig.LOGIN_BTN_TEXT)
                .setSloganOffsetY(65)
                .setSloganTextSize(12)
                .setSloganTextColor(Color.parseColor("#FF999999"))

                .setLogBtnOffsetY(120)
                .setLogBtnHeight(44)
                .setLogBtnMarginLeftAndRight(30)
                .setLogBtnTextSize(20)
                .setLogBtnText(OneKeyConfig.LOGIN_BTN_TEXT)
//                .setLogBtnBackgroundPath("login_btn_bg")
                .setSwitchAccHidden(true)
                .setSwitchOffsetY(0)
                .setSwitchAccText("")
                .setSwitchAccTextColor(Color.parseColor("#666666"))
                .setSwitchAccTextSize(12)
                .setPageBackgroundPath("dialog_page_background")
                .setAuthPageActIn("in_activity", "out_activity")
                .setAuthPageActOut("in_activity", "out_activity")
                .setVendorPrivacyPrefix("《")
                .setVendorPrivacySuffix("》")
                .setPrivacyBefore("点击“登录”即同意")
                .setPrivacyEnd("并使用本机号码登录")
                .setPrivacyOffsetY_B(6)
                .setDialogHeight(dialogHeight)
                .setDialogBottom(true)
                .setStatusBarColor(color)
                .setWebNavColor(color)
                .setDialogWidth(mScreenWidthDp)
                .setScreenOrientation(authPageOrientation)
                .create());
    }
}
