package com.qhclh.ytzh.login;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.configs.ConfigUtil;
import com.qhclh.ytzh.configs.LoginUserConfig;
import com.qhclh.ytzh.configs.ServerConfig;
import com.qhclh.ytzh.home.MainActivity;
import com.qhclh.ytzh.home.MyApplication;
import com.qhclh.ytzh.home.SetUrlActivity;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.utils.ActivityUtil;
import com.qhclh.ytzh.utils.DialogUtil;
import com.qhclh.ytzh.utils.HdHttpUtil;

import org.forks.jsonrpc.RpcFacade;

import java.util.LinkedHashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by 青花瓷 on 2017/11/28.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.login_username)
    EditText login_username;
    @BindView(R.id.login_psw)
    EditText login_psw;
    @BindView(R.id.login_sure)
    Button login_sure;

    @BindView(R.id.shezhiurl)
    TextView shezhiurl;

    private LoginUserConfig config = new LoginUserConfig(this);
    private ServerConfig sconfig = new ServerConfig(this);

    @Override
    protected int setLayoutId() {
        return R.layout.act_login;
    }

    @Override
    protected void initView() {
        loginOut.execute();
        MyApplication.ClearLogin();
        ConfigUtil.Fill(sconfig);
        ConfigUtil.Fill(config);
        login_username.setText(config.Username);
        login_psw.setText(config.Password);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

    @OnClick({R.id.login_sure,R.id.shezhiurl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_sure:
                if (HdHttpUtil.isNetworkAvailable(LoginActivity.this)) {
                    loginTask.execute();
                } else {
                    DialogUtil.AutoHiddenAlert(LoginActivity.this, "网络不可用");
                }
                break;
            case R.id.shezhiurl:
                startActivity(new Intent(this, SetUrlActivity.class));
                break;
        }
    }


    ViewOnClickTask loginOut = new ViewOnClickTask(this, "") {

        @Override
        public Object call() throws Exception {
            RpcFacade.logout();
            return null;
        }

        @Override
        protected void successUI() {

        }
    };
    ViewOnClickTask loginTask = new ViewOnClickTask(this, "登录中...") {
        public Object call() throws Exception {
            String username = login_username.getText().toString();
            String password = login_psw.getText().toString();
            config.Username = username;
            config.Password = password;
            RpcFacade.logout();
            RpcFacade.login(username, password);
            config.DisplayName = username;
            ConfigUtil.Save(config);
            return null;
        }

        @Override
        protected void successUI() {
            ConfigUtil.Fill(sconfig);
            Set<String> setTags = new LinkedHashSet<>();
            setTags.add(config.Username);
            JPushInterface.setTags(LoginActivity.this, 0, setTags);
            ActivityUtil.Go(LoginActivity.this, MainActivity.class);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //按返回键，程序进入后台运行，不关闭程序
            MyApplication.getInstance().exit();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
