package com.example.jinyu_rfid;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;


import com.example.jinyu_rfid.base.BaseActivity;
import com.example.jinyu_rfid.base.MyRecultCall;
import com.example.jinyu_rfid.base.MyResult;
import com.example.jinyu_rfid.been.ConfigurationTable;
import com.example.jinyu_rfid.been.LoginResult;
import com.example.jinyu_rfid.databinding.ActivityMainBinding;
import com.example.jinyu_rfid.uitls.SharedPreferencesUtils;
import com.example.jinyu_rfid.been.LoginVm;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.litepal.LitePal;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import okhttp3.RequestBody;

public class MainActivity extends BaseActivity {
    private LoginVm loginVm;
    private ActivityMainBinding binding;

    private Resources resources;
    private Configuration config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resources = getResources();
        config = resources.getConfiguration();
        var equals = SharedPreferencesUtils.getstring("language", "English").equals("中文");
        config.locale = equals ? Locale.CHINA : Locale.ENGLISH;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        // binding.mainLanguage.setSelection(equals?1:0);
        loginVm = new LoginVm();
        boolean isRemember = SharedPreferencesUtils.getboolean("isRemember", false);
        if (isRemember) {
            loginVm.setUsername(SharedPreferencesUtils.getstring("user", ""));
            loginVm.setPassword(SharedPreferencesUtils.getstring("pass", ""));
            loginVm.setRemember(isRemember);
        }
        binding.setUser(loginVm);


    }


    @Override
    public void sanCodeCall(String code) {

    }

    public void login_click(View view) {
        String pass = loginVm.getPassword();
        String name = loginVm.getUsername();
        var selectLanguage = binding.mainLanguage.getSelectedItem().toString();
        config.locale = selectLanguage.equals("中文") ? Locale.CHINA : Locale.ENGLISH;


        Map<String, String> map = new HashMap<>();
        map.put("UserID", name);
        map.put("UserPass", pass);
        map.put("token", "123456");
        if (pass == null || name == null || pass.isEmpty() || name.isEmpty()) return;
        OkGo.<MyResult>post(url + "/login")
                .upRequestBody(RequestBody.create(JSON, gson.toJson(map)))
                .execute(new MyRecultCall(dialog, this) {
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        var body = response.body();
                        if (body.getExcuteFlag() == 1) {
                            LoginResult loginResult = gson.fromJson(body.getJson(), LoginResult.class);
                            if (loginResult.getVerifyFlag().equals("1")) {
                                boolean remember = loginVm.isRemember();
                                if (remember) {
                                    SharedPreferencesUtils.putboolean("isRemember", remember);
                                    SharedPreferencesUtils.putstring("user", name);
                                    SharedPreferencesUtils.putstring("pass", pass);
                                }
                                SharedPreferencesUtils.putstring("language", selectLanguage);
                                resources.updateConfiguration(config, resources.getDisplayMetrics());
                                List<ConfigurationTable> list = LitePal.findAll(ConfigurationTable.class);
                                Intent intent;
                                if (list==null||list.isEmpty()){
                                    intent = new Intent(MainActivity.this, ConfigurationTableActivity.class);
                                }else {
                                    intent = new Intent(MainActivity.this, WriteUserActivity.class);
                                }
                                startActivity(intent);
                                finish();

                            }
                            Toast.makeText(MainActivity.this, loginResult.getVerifyMessage(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }
}
