package com.gingersoft.gsa.cloud.user.login.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.gingersoft.gsa.cloud.base.utils.constans.UserConstans;
import com.gingersoft.gsa.cloud.base.utils.encryption.Aes;
import com.gingersoft.gsa.cloud.base.utils.other.SPUtils;
import com.gingersoft.gsa.cloud.ui.bean.mode.LoginBean;
import com.gingersoft.gsa.cloud.ui.widget.dialog.ChooseRestaurantDialog;
import com.gingersoft.gsa.cloud.user.login.R;
import com.gingersoft.gsa.cloud.user.login.di.component.DaggerWelcomeComponent;
import com.gingersoft.gsa.cloud.user.login.di.module.WelcomeModule;
import com.gingersoft.gsa.cloud.user.login.mvp.contract.LoginInterfaceImpl;
import com.gingersoft.gsa.cloud.user.login.mvp.contract.WelcomeContract;
import com.gingersoft.gsa.cloud.user.login.mvp.presenter.WelcomePresenter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class WelcomeActivity extends LoginInterfaceImpl<WelcomePresenter> implements WelcomeContract.View {
    @BindView(R.id.content_view)
    ConstraintLayout contentView;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerWelcomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .welcomeModule(new WelcomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_welcome; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ScaleAnimation animation = new ScaleAnimation(1.0f, 1.06f, 1.0f, 1.06f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        contentView.setAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                startActivity(new Intent(mContext, LoginActivity.class));
                if ((boolean) SPUtils.get(mContext, UserConstans.AUTO_LOGIN, false)) {
                    //自動登陸
                    String pwd = Aes.aesDecrypt((String) SPUtils.get(mContext, UserConstans.LOGIN_PASSWORD, ""));
                    mPresenter.login(SPUtils.get(mContext, UserConstans.LOGIN_USERNAME, "") + "", pwd);
                } else {
                    startActivity(new Intent(mContext, LoginActivity.class));
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    public void initIntent() {

    }

    @Override
    public void initTopBar() {

    }

    @Override
    public void initLanguage() {

    }

    @Override
    public void initLayoutParams() {

    }

    @Override
    public void initLayoutVisible() {

    }


    @Override
    public void showLoading(String message) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void loginSuccess(LoginBean info) {
        super.loginSuccess(info);
    }

    private boolean isChooseRestaurant = false;

    @Override
    public void showChooseRestaurantDialog(List<LoginBean.DataBean.UserBean.BrandsBean> brandsBeans, List<LoginBean.DataBean.UserBean.BrandsBean> brands) {
        new ChooseRestaurantDialog.BottomListSheetBuilder(mContext)
                .addBrandItems(brandsBeans)
                .setOnItemClickListener((dialog, item, position) -> {
                    isChooseRestaurant = true;
                    ChooseRestaurantItemClick(brands, dialog, item);
                })
                .setOnDismissListener(dialog -> {
                    if (!isChooseRestaurant) {
                        //沒有選擇餐廳，就回到登陸頁面
                        startActivity(new Intent(mContext, LoginActivity.class));
                    }
                    killMyself();
                })
                .build()
                .show();

    }
}
