package com.fusi24.entryPermitScanner.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.fusi24.entryPermitScanner.R;
import com.fusi24.entryPermitScanner.base.BaseActivity;
import com.fusi24.entryPermitScanner.data.entity.DataDirectURL;
import com.fusi24.entryPermitScanner.data.entity.DataSerialPort;
import com.fusi24.entryPermitScanner.databinding.ActivitySettingBinding;
import com.fusi24.entryPermitScanner.ui.home.HomeActivity;
import com.fusi24.entryPermitScanner.ui.login.LoginActivity;

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

public class SettingActivity extends BaseActivity {

    public static final String IS_FROM_LOGIN = "is_from_login"; // <== To define from Login Page or Home Page
    private DataSerialPort dataSerialPort;
    private DataDirectURL dataDirectURL;
    private List<DataSerialPort> serialPortList;
    private List<DataDirectURL> directURLList;
    private ActivitySettingBinding binding;
    boolean isFromLogin = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        System.out.println("DEVELOPER Setting onCreate");

        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView toolbarTitle = findViewById(R.id.toolbar_text);

        if (toolbarTitle != null && toolbar != null) {
            toolbarTitle.setText(getString(R.string.label_setting));
            setSupportActionBar(toolbar);
        }

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        if (getIntent().getExtras() != null){
            isFromLogin = getIntent().getExtras().getBoolean(IS_FROM_LOGIN);
        }

        dataSerialPort = new DataSerialPort();
        dataDirectURL = new DataDirectURL();
        serialPortList = new ArrayList<>();
        directURLList = new ArrayList<>();

        getDataSerialPort();
        getDataDirectURL();
        initEvent();
    }

    private void getDataSerialPort() {
        serialPortList.add(new DataSerialPort(1, "Serial Port 2"));
        serialPortList.add(new DataSerialPort(2, "Serial Port 3"));
        setDataSerialPort();
    }

    private void getDataDirectURL() {
        directURLList.add(new DataDirectURL(1, "Development"));
        directURLList.add(new DataDirectURL(2, "Production"));
        setDataDirectURL();
    }


    private void setDataSerialPort(){
        ArrayAdapter<DataSerialPort> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, serialPortList);
        binding.spinnerSerialPort.setAdapter(adapter);
        if (getSessionManager().isSerialPort2()){
            binding.spinnerSerialPort.setSelection(0);
        } else {
            binding.spinnerSerialPort.setSelection(1);
        }
    }

    private void setDataDirectURL(){
        ArrayAdapter<DataDirectURL> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, directURLList);
        binding.spinnerDirectUrl.setAdapter(adapter);
        if (getSessionManager().isDevelopment()){
            binding.spinnerDirectUrl.setSelection(0);
        } else {
            binding.spinnerDirectUrl.setSelection(1);
        }
    }

    private void initEvent(){

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

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

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

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

        binding.btnScan.setOnClickListener(v -> {

            Toast.makeText(this, dataSerialPort.getName() + " " + dataDirectURL.getName(), Toast.LENGTH_LONG).show();

            // ID 1 ==> Serial Port 2
            if (dataSerialPort.getId() == 1){
                getSessionManager().setIsSerialport2(true);
            } else {
                getSessionManager().setIsSerialport2(false);
            }

            // ID 1 ==> Development
            if (dataDirectURL.getId() == 1){
                getSessionManager().setIsDevelopment(true);
            } else {
                getSessionManager().setIsDevelopment(false);
            }

            Intent intent;
            if (isFromLogin){
                intent = new Intent(this, LoginActivity.class);
            } else {
                intent = new Intent(this, HomeActivity.class);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
