package com.philisense.rdgo.ui.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.LoginBean;
import com.philisense.rdgo.databinding.ActivityLoginBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.ui.MainActivity;
import com.philisense.rdgo.viewModel.LoginViewModel;

import java.util.ArrayList;

import io.reactivex.functions.Consumer;


public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding mbinding;
    private LoginViewModel viewModel;
    private DeviceUtils deviceUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mbinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        initSpinner();
        deviceUtils = new DeviceUtils(this);
        String uuid = deviceUtils.getUniqueID();
//        System.out.println(uuid + "ppppp");
        viewModel = new LoginViewModel(uuid,
                this,
                RxTextView.textChanges(mbinding.loginUsernameEdit),
                RxTextView.textChanges(mbinding.loginPasswordEdit));

        // 先获取第一种状态
        didSelectUserListItem(0);

        // 登录验证
        viewModel.loginValidation.subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                mbinding.loginButton.setEnabled(aBoolean);
            }
        });

        RxView.clicks(mbinding.loginButton).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                final ProgressDialog dialog = ProgressDialog.show(LoginActivity.this,"提示","正在登录..");
                viewModel.loginAction(new RequestImpl() {
                    @Override
                    public void loadSuccess(Object object) {
                        dialog.dismiss();
                        LoginBean bean = (LoginBean) object;
                        Snackbar.make(mbinding.loginLayout,bean.getMessage(),Snackbar.LENGTH_LONG).show();
//                        System.out.println(bean.getData().getToken()+"ppppppppppppp");
                        MainActivity.start(LoginActivity.this);
                        finish();
                    }

                    @Override
                    public void loadFailed(OError error) {
                        dialog.dismiss();
                        showError(mbinding,error);
                    }
                });
            }
        });
    }

    /**
     * 初始化下拉框
     */
    public void initSpinner() {
//        ArrayList<String> list = new ArrayList<>();
//        list.add("产线");
//        list.add("入库");
//        list.add("仓库");
//        list.add("稽查");
//
//        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);
//        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
//        mbinding.loginSpinner.setAdapter(adapter);
//        mbinding.loginSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                didSelectUserListItem(i);
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//            }
//        });
    }

    /**
     * 选择登录用户类别
     * @param index 第几个类别
     */
    public void didSelectUserListItem(int index) {
        viewModel.getUserList(index, new RequestImpl<ArrayList<String>>() {
            @Override
            public void loadSuccess(ArrayList<String> list) {
                addUsernameToAutoComplete(list);
            }

            @Override
            public void loadFailed(OError error) {
                showError(mbinding,error);
            }
        });
    }

    /**
     * 网络请求错误
     */
    public void showError(ActivityLoginBinding binding, OError error) {
        if (error.getErrorCode() == 1) {
            if ("密码错误!".equals(error.getErrorMsg())) {
                binding.loginPasswordEdit.requestFocus();
                binding.loginPasswordEdit.setError(error.getErrorMsg());
            }else {
                binding.loginUsernameEdit.requestFocus();
                binding.loginUsernameEdit.setError(error.getErrorMsg());
            }
        }else {
            Snackbar.make(binding.loginLayout,error.getErrorMsg(),Snackbar.LENGTH_LONG).show();
        }
    }

    public static void start(Activity context) {
        Intent intent = new Intent(context, LoginActivity.class);
        ActivityCompat.startActivity(context, intent,null);
    }

    /**
     * 添加用户信息到自动完成
     * @param usernameCollection 用户信息数组
     */
    private void addUsernameToAutoComplete(ArrayList<String> usernameCollection) {
        ArrayAdapter<String> adapter =
                new ArrayAdapter<>(this,
                        android.R.layout.simple_dropdown_item_1line, usernameCollection);
        mbinding.loginUsernameEdit.setAdapter(adapter);
    }


}
