package com.fusi24.rfid.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.fusi24.rfid.R;
import com.fusi24.rfid.data.SessionManager;
import com.fusi24.rfid.data.entity.DataAuth;
import com.fusi24.rfid.data.entity.DataUser;
import com.fusi24.rfid.util.Helper;
import com.fusi24.rfid.util.LFScanProcessing;

import timber.log.Timber;

public class LoginActivity extends LFScanProcessing implements LoginView {

    private SessionManager session;
    private LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        presenter = new LoginPresenter(getManager(), getAndroidScheduler(), getProcessScheduler());
        presenter.attachView(this);
        session = new SessionManager(Helper.getDefaultPreferences(this));

        initSound();
        onStart();
    }

    void startInstance(String numberRfidTemp){
        DataAuth dataAuth = new DataAuth();
        dataAuth.setRfidCode(Helper.formatRfidNumber(numberRfidTemp));
        presenter.userLogin(dataAuth);
    }

    // This method for read chip number in the card
    @Override
    protected void onDataReceived(byte[] buffer, int size) {
        runOnUiThread(() -> {
            if (rfidCardNumber != null) {
                if (size > 0) {
                    if (size >= 5) {
                        byte[] tempBuf = new byte[size - 4];
                        System.arraycopy(buffer, 4, tempBuf, 0, size - 4);//discard 4 bit
                        String temStr = new String(tempBuf);
                        int temInt = Integer.parseInt(temStr, 16);
                        soundpool.play(soundid, 1, 1, 0, 0, 1);
                        rfidCardNumber = Integer.toString(temInt);

                        Timber.i("Hasil dari RFID LF: %s", rfidCardNumber);
                        startInstance(rfidCardNumber);
                    } else {
                        // receipt data 4 digit to start
                        rfidCardNumber = new String(buffer, 0, size);
                    }
                }
            }
        });
    }

    @Override
    public void showLoginSuccess(DataUser dataUser) {
        if (dataUser.isSuccess()){
            session.setUserLogin();
            session.setUserId(Integer.valueOf(dataUser.getIdUser()));
            session.setUserName(dataUser.getUsername());
            session.setEmployeeName(dataUser.getNamaKaryawan());
            session.setUserSid(dataUser.getKodeKaryawan());
            session.setUserEmail(dataUser.getEmail());
            session.setUserTelephone(dataUser.getSelular());
            session.setUserRfidNumber(Helper.formatRfidNumber(rfidCardNumber));

            Intent result = new Intent();
            setResult(Activity.RESULT_OK, result);
            finish();
        } else {
            Intent intent = new Intent(this, ErrorLoginActivity.class);
            if (dataUser.getMessage() != null){
                intent.putExtra(ErrorLoginActivity.MESSAGE, dataUser.getMessage());
            }
            startActivity(intent);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
    }

    @Override
    public void showError(Throwable e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
