package com.fusi24.rfid;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.fusi24.rfid.base.BaseActivity;
import com.fusi24.rfid.config.Constant;
import com.fusi24.rfid.data.api.RestService;
import com.fusi24.rfid.data.api.RestServiceFactory;
import com.fusi24.rfid.data.entity.DataEntryPermit;
import com.fusi24.rfid.data.entity.DataSite;
import com.fusi24.rfid.ui.ScanActivity;

import org.jetbrains.annotations.NotNull;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends BaseActivity {

    @BindView(R.id.btn_scan)
    Button btnScan;
    @BindView(R.id.spinner_site)
    Spinner spinnerSite;
    @BindView(R.id.spinner_permit)
    Spinner spinnerPermit;

    private DataSite dataSite;
    private DataEntryPermit dataEntryPermit;
    private List<DataSite> siteList;
    private List<DataEntryPermit> entryPermitList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        dataSite = new DataSite();
        dataEntryPermit = new DataEntryPermit();

        siteList = new ArrayList<>();
        entryPermitList = new ArrayList<>();

        getDataSite();
        getDataPermit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initEvent();

    }

    private void getDataSite() {
        RestService restService = RestServiceFactory.create(Constant.JWT_TOKEN);
        Call<List<DataSite>> call = restService.getDataSite(100);
        call.enqueue(new Callback<List<DataSite>>() {
            @Override
            public void onResponse(@NotNull Call<List<DataSite>> call, @NotNull Response<List<DataSite>> response) {
                if (response.body() != null){
                    siteList.addAll(response.body());
                    setDataSite();
                }
            }

            @Override
            public void onFailure(@NotNull Call<List<DataSite>> call, @NotNull Throwable t) {
                Toast.makeText(MainActivity.this, "Failed to get data site", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getDataPermit() {
        RestService restService = RestServiceFactory.create(Constant.JWT_TOKEN);
        Call<List<DataEntryPermit>> call = restService.getEntryPermit();
        call.enqueue(new Callback<List<DataEntryPermit>>() {
            @Override
            public void onResponse(@NotNull Call<List<DataEntryPermit>> call, @NotNull Response<List<DataEntryPermit>> response) {
                if (response.body() != null) {
                    entryPermitList.addAll(response.body());
                    setDataPermit();
                }
            }

            @Override
            public void onFailure(@NotNull Call<List<DataEntryPermit>> call, @NotNull Throwable t) {
                Toast.makeText(MainActivity.this, "Failed to get data permit", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setDataSite(){
        ArrayAdapter<DataSite> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, siteList);
        spinnerSite.setAdapter(adapter);
    }

    private void setDataPermit(){
        ArrayAdapter<DataEntryPermit> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, entryPermitList);
        spinnerPermit.setAdapter(adapter);
    }

    private void initEvent(){

        spinnerSite.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                dataSite = (DataSite) parent.getItemAtPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                dataSite = (DataSite) parent.getSelectedItem();
            }
        });

        spinnerPermit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                dataEntryPermit = (DataEntryPermit) parent.getItemAtPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                dataEntryPermit = (DataEntryPermit) parent.getSelectedItem();
            }
        });

        btnScan.setOnClickListener(v -> {

            if (spinnerSite.getSelectedItem() == null){
                Toast.makeText(this, "Data Site Belum Terpilih", Toast.LENGTH_SHORT).show();
                return;
            }

            if (spinnerPermit.getSelectedItem() == null){
                Toast.makeText(this, "Data Permit Belum Terpilih", Toast.LENGTH_SHORT).show();
                return;
            }

            Intent intent = new Intent(this, ScanActivity.class);
            intent.putExtra(ScanActivity.ID_SITE, String.valueOf(dataSite.getId()));
            intent.putExtra(ScanActivity.ID_PERMIT, String.valueOf(dataEntryPermit.getId()));
            startActivity(intent);
        });
    }

}
