package xn.xxp.home.auth;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.FragmentManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.FragmentUtils;

import java.util.ArrayList;
import java.util.List;

import core.ui.activity.BaseSignActivity;
import http.vo.response.UserVo;
import xn.xxp.HomeActivity;
import xn.xxp.R;
import xn.xxp.app.LabApp;
import xn.xxp.app.SyncFaceTool;
import xn.xxp.databinding.ActivityAuthChoiceBinding;
import xn.xxp.home.AccessVerify;
import xn.xxp.home.auth.fragment.CardAuthFragment;
import xn.xxp.home.auth.fragment.LzFaceAuthFragment;
import xn.xxp.home.auth.fragment.QrAuthFragment;
import xn.xxp.home.notice.NoticeListActivity;
import xn.xxp.main.MainActivity;
import xn.xxp.room.RoomTool;
import xn.xxp.room.bean.DeviceConfig;
import xn.xxp.room.bean.LabConfig;
import xn.xxp.room.bean.NoticeSummary;
import xn.xxp.room.dao.NoticeSummaryDao;
import xn.xxp.utils.Tool;
import xn.xxp.widget.ITitleBar;
import xn.xxp.widget.NavViewCompat;

public class ChoiceAuthActivity extends BaseSignActivity<ActivityAuthChoiceBinding> {

    private String accessVerify;
    private FragmentManager fragmentManager;
    private LabConfig labConfig;
    private DeviceConfig deviceConfig;

    @Override
    public ITitleBar getMTitleBar() {
        return null;
    }

    @Override
    public NavViewCompat getMNavView() {
        return binding.navView;
    }

    @Override
    protected boolean enabledBackCountDown() {
        return true;
    }

    @Override
    protected ActivityAuthChoiceBinding createViewBinding() {
        return ActivityAuthChoiceBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        binding.titleBar.setTitleInfoFromSp();
        NoticeSummaryDao noticeSummaryDao = RoomTool.getInstance().noticeSummaryDao();
        List<NoticeSummary> noticeSummaryList = noticeSummaryDao.getAll();
        if (null == noticeSummaryList) {
            noticeSummaryList = new ArrayList<>();
        }
        binding.titleBar.updateNotice(noticeSummaryList);

        deviceConfig = RoomTool.getInstance().deviceConfigDao().getDeviceConfig();
        labConfig = RoomTool.getInstance().labConfigDao().getLabConfig();
        String access = getIntent().getStringExtra("access");
        accessVerify = null == access ? AccessVerify.NORMAL.getType() : access;
        fragmentManager = getSupportFragmentManager();
        FragmentUtils.add(fragmentManager, new LzFaceAuthFragment(), R.id.container);

        // 切换为人脸
        binding.faceAuth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(FragmentUtils.getTop(fragmentManager) instanceof LzFaceAuthFragment)) {
                    FragmentUtils.replace(fragmentManager, new LzFaceAuthFragment(), R.id.container);
                }
            }
        });
        //  切换为刷卡
        binding.cardAuth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(FragmentUtils.getTop(fragmentManager) instanceof CardAuthFragment)) {
                    FragmentUtils.replace(fragmentManager, new CardAuthFragment(), R.id.container);
                }
            }
        });

        // 切换为扫码
        binding.qrAuth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(FragmentUtils.getTop(fragmentManager) instanceof QrAuthFragment)) {
                    FragmentUtils.replace(fragmentManager, new QrAuthFragment(), R.id.container);
                }
            }
        });
    }

    public void authSuccess(AuthType authType, UserVo user) {
        LabApp.userVo = user;
        if (accessVerify.equals(AccessVerify.NORMAL.getType())) {
            ActivityUtils.startActivity(MainActivity.class);
            finish();
        } else if (accessVerify.equals(AccessVerify.SIGN_IN.getType())) {
            dispatchSignIn();
        } else if (accessVerify.equals(AccessVerify.SIGN_OUT.getType())) {
            dispatchSignOut();
        } else if (accessVerify.equals(AccessVerify.JUST_OPEN_DOOR.getType())) {
            Tool.INSTANCE.openDoor();
            ActivityUtils.finishToActivity(HomeActivity.class, false);
        }
    }

    @Override
    protected void onSignInFinish(boolean completed) {
        super.onSignInFinish(completed);
        if (completed) {
            LabApp.userVo = null;
            Tool.INSTANCE.openDoor();
        }
        finish();
    }

    @Override
    protected void onSignOutFinish(boolean completed) {
        super.onSignOutFinish(completed);
        if (completed) LabApp.userVo = null;
        finish();
    }


}
