package cn.yyxx.commsdk.platform.hnyy;

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.platform.LoginViewModel;
import cn.yyxx.commsdk.platform.hnyy.dialog.AutoLoginDialog;
import cn.yyxx.commsdk.platform.hnyy.floating.FloatViewManager;
import cn.yyxx.commsdk.platform.hnyy.fragment.UserAccountLoginFragment;
import cn.yyxx.commsdk.platform.hnyy.fragment.UserPhoneLoginFragment;
import cn.yyxx.commsdk.platform.hnyy.fragment.UserPhoneLoginSetpwdFragment;
import cn.yyxx.commsdk.platform.hnyy.fragment.UserRegisterFragment;
import cn.yyxx.commsdk.platform.hnyy.fragment.UserRetrievePwdFragment;
import cn.yyxx.commsdk.platform.hnyy.fragment.UserSwitchAccountLoginFragment;
import cn.yyxx.commsdk.platform.hnyy.impl.SdkPlatformImpl;
import cn.yyxx.commsdk.platform.user.UserSessionManager;
import cn.yyxx.support.AndroidBug5497Workaround;
import cn.yyxx.support.AppUtils;
import cn.yyxx.support.ResUtils;

public class HNYYLoginActivity extends BaseLoginActivity {




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

    public static LoginViewModel loginViewModel;

    @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 = ResUtils.getResId(this, "hnyy_login_container", "id");
        initView(this);
        if (UserSessionManager.getInstance().getLocalLastSession() != null){
            autoLogin(this);
        }else{
            if (SdkPlatformImpl.initConfig.isShowPhoneRegFrist){
                switchFragment(HNYYLoginFragmentTag.PAGE_PHONE_LOGIN,null);
            }else{
                switchFragment(HNYYLoginFragmentTag.PAGE_ACCOUNT_REGISTER,null);
            }
        }

    }

    public static void start(Activity activity, ICallback callback) {
        loginViewModel = new LoginViewModel(callback);
        activity.startActivity(new Intent(activity, HNYYLoginActivity.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(ResUtils.getResId(this, "hnyy_login", "layout"));

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


    }

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

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

               switchFragment(userPhoneLoginSetpwdFragment,tag,bundle);
               break;
           case HNYYLoginFragmentTag.PAGE_ACCOUNT_REGISTER:
               if (userRegisterFragment == null){
                   userRegisterFragment = new UserRegisterFragment();
               }
               switchFragment(userRegisterFragment,tag,bundle);
               break;
           case HNYYLoginFragmentTag.PAGE_RETRIEVE_PASSWORD:
               if (userRetrievePwdFragment == null){
                   userRetrievePwdFragment = new UserRetrievePwdFragment();
               }
               switchFragment(userRetrievePwdFragment,tag,bundle);
               break;
           case HNYYLoginFragmentTag.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,loginViewModel);
        builder.setPositiveButton(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //取消回调
                dialog.dismiss();

                switchFragment(HNYYLoginFragmentTag.PAGE_SWITCH_ACCOUNT_LOGIN,null);


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


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

        builder.Create().show();
    }

}
