package com.example.audiovisualrecord.ui.login;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.audiovisualrecord.ui.main.MainActivity;
import com.example.audiovisualrecord.R;
import com.example.audiovisualrecord.base.BaseActivity;

public class LoginActivity extends BaseActivity implements LoginContract.View, View.OnClickListener {
    EditText editAccount, editPassword;
    Button btnLogin;
    private LoginPresenter presenter;


//    LoginContract.Presenter<LoginContract.View> mPresenter = new LoginContract.Presenter<LoginContract.View>;

    private static final int REQUEST_PERMISSIONS_CODE = 20200410;
    private String[] permissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA};

    @RequiresApi(api = Build.VERSION_CODES.M)

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        init();
        requestPermissions(permissions, REQUEST_PERMISSIONS_CODE);
        presenter = new LoginPresenter(this);


    }

    @Override
    protected void onPause() {
        super.onPause();
        btnLogin.setEnabled(true);
    }

    @Override
    public void init() {
        editAccount = findViewById(R.id.edit_account);
        editPassword = findViewById(R.id.edit_password);
        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                btnLogin.setEnabled(false);
                showProgressDialog("登入中");
                if (onCheckUserisEmpty()) {

                    presenter.onLogin(editAccount.getText().toString(), editPassword.getText().toString());
                } else {
                    dismissProgressDialog();
                    btnLogin.setEnabled(true);
                }
                break;
        }
    }

    private boolean onCheckUserisEmpty() {
        if ("".equals(editAccount.getText().toString())) {
            showDialogMessage(getResourceString(R.string.login_account_hint));
            return false;
        }
        if ("".equals(editPassword.getText().toString())) {
            showDialogMessage(getResourceString(R.string.login_password_hint));
            return false;
        }
        return true;
    }

    @Override
    public void onCompleteLogin(String token) {
        dismissProgressDialog();
        Intent intent = new Intent(this, MainActivity.class);
        Bundle bag = new Bundle();
        bag.putString("token", token);
        intent.putExtras(bag);
        startActivity(intent);
    }

    @Override
    public void setButtonEnable(Boolean boo) {
        btnLogin.setEnabled(boo);
    }
}
