package cn.yyxx.commsdk.impl.merge.platform.xinrui;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.dialog.AutoLoginDialog;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.UserAccountLoginFragment;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.UserPhoneLoginFragment;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.UserPhoneLoginSetpwdFragment;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.UserRegisterFragment;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.UserRetrievePwdFragment;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.fragment.UserSwitchAccountLoginFragment;
import cn.yyxx.commsdk.impl.merge.platform.xinrui.impl.SdkPlatformImpl;
import cn.yyxx.commsdk.impl.merge.user.UserSessionManager;
import cn.yyxx.support.AndroidBug5497Workaround;
import cn.yyxx.support.AppUtils;

public class XinRuiLoginActivity extends BaseLoginActivity {




    private UserAccountLoginFragment userAccountLoginFragment;
    private UserPhoneLoginFragment userPhoneLoginFragment;
    private UserPhoneLoginSetpwdFragment userPhoneLoginSetpwdFragment;
    private UserRegisterFragment userRegisterFragment;
    private UserRetrievePwdFragment userRetrievePwdFragment;
    private UserSwitchAccountLoginFragment userSwitchAccountLoginFragment;





    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);


            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }

        containerId = ResourceUtil.getId(this, "login_container");

        initView(this);
        if (UserSessionManager.getInstance().getLocalLastSession() != null){
            if (SdkPlatformImpl.initConfig.isSwitchAccount){
                switchFragment(XinRuiLoginFragmentTag.PAGE_SWITCH_ACCOUNT_LOGIN,null);

            }else{
                autoLogin(this);
            }

        }else{
            if (SdkPlatformImpl.initConfig.isShowPhoneRegFrist){
                switchFragment(XinRuiLoginFragmentTag.PAGE_PHONE_LOGIN,null);
            }else{
                switchFragment(XinRuiLoginFragmentTag.PAGE_ACCOUNT_REGISTER,null);
            }
        }

    }

    public static void start(Activity activity, ICallback callback) {

        activity.startActivity(new Intent(activity, XinRuiLoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }

    private void initView(Context context){
        if (AppUtils.isLandscape(context)) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        }

        setContentView(ResourceUtil.getLayoutId(this,"xinrui_login"));

        if (!AppUtils.isLandscape(context)) {
            AndroidBug5497Workaround.assistActivity(this);
        }


    }

    @Override
    public void switchFragment(final String tag,Bundle bundle) {
        super.switchFragment(tag,bundle);
       switch (tag){
           case XinRuiLoginFragmentTag.PAGE_ACCOUNT_LOGIN:
               if (userAccountLoginFragment == null){
                   userAccountLoginFragment = new UserAccountLoginFragment();
               }
               switchFragment(userAccountLoginFragment,tag,bundle);
               break;
           case XinRuiLoginFragmentTag.PAGE_PHONE_LOGIN:
               if (userPhoneLoginFragment == null){
                   userPhoneLoginFragment = new UserPhoneLoginFragment();
               }

               switchFragment(userPhoneLoginFragment,tag,bundle);
               break;
           case XinRuiLoginFragmentTag.PAGE_POHONE_SET_PASSWORD:
               if (userPhoneLoginSetpwdFragment == null){
                   userPhoneLoginSetpwdFragment = new UserPhoneLoginSetpwdFragment();
               }

               switchFragment(userPhoneLoginSetpwdFragment,tag,bundle);
               break;
           case XinRuiLoginFragmentTag.PAGE_ACCOUNT_REGISTER:
               if (userRegisterFragment == null){
                   userRegisterFragment = new UserRegisterFragment();
               }
               switchFragment(userRegisterFragment,tag,bundle);
               break;
           case XinRuiLoginFragmentTag.PAGE_RETRIEVE_PASSWORD:
               if (userRetrievePwdFragment == null){
                   userRetrievePwdFragment = new UserRetrievePwdFragment();
               }
               switchFragment(userRetrievePwdFragment,tag,bundle);
               break;
           case XinRuiLoginFragmentTag.PAGE_SWITCH_ACCOUNT_LOGIN:
               if (userSwitchAccountLoginFragment == null){
                   userSwitchAccountLoginFragment = new UserSwitchAccountLoginFragment();
               }
               switchFragment(userSwitchAccountLoginFragment,tag,bundle);
               break;

       }
    }

    private void autoLogin(final Activity activity) {
        AutoLoginDialog.Builder builder = new AutoLoginDialog.Builder(activity,SdkPlatformImpl.loginViewModel);
        builder.setPositiveButton(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //取消回调
                dialog.dismiss();

                switchFragment(XinRuiLoginFragmentTag.PAGE_SWITCH_ACCOUNT_LOGIN,null);


            }
        }).setCloseDialogCallback(new AutoLoginDialog.CloseDialogCallback() {


            @Override
            public void onLoginSuccess() {
                if (!isFinishing()) {
                     finish();
                     SdkPlatformImpl.getInstance(activity).loginSuccess();
                }
            }
            @Override
            public void onLoginFail() {
                switchFragment(XinRuiLoginFragmentTag.PAGE_SWITCH_ACCOUNT_LOGIN,null);
            }
        });

        builder.Create().show();
    }

}
