package com.mq.oaapp.ui;

import android.content.Intent;
import android.net.Uri;
import android.widget.EditText;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.mq.oaapp.R;
import com.mq.oaapp.UrlPath;
import com.mq.oaapp.base.BaseActivity;
import com.mq.oaapp.utils.MyConstant;
import com.xyxsbj.mylibrary.tool.ETTool;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.SPTool;
import com.xyxsbj.mylibrary.tool.StringTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建人 mQ
 * 创建时间 2019/9/24 0024 18:02
 * 文件名称 LoginActivity
 * 说明:
 **/
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_path)
    EditText mEtPath;
    @BindView(R.id.et_user)
    EditText mEtUser;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;

    @Override
    protected int getLayoutRes() {
        setActivityLightOrDark(false);
        return R.layout.activity_login;
    }

    @Override
    protected void init() {

        //检测是否自动登录
        String autoLogin = (String) SPTool.get(MyConstant.AUTO_LOGIN, "");
        String userCode = (String) SPTool.get(MyConstant.USER_CODE, "");
        if (autoLogin.equals("0") && !userCode.equals("")) {
            //开启自动登录，且用户不为空，则进入主页
            startHome(userCode);
        }
    }

    @Override
    protected void workTask() {
        mEtUser.setText((String) SPTool.get(MyConstant.USER_NAME, ""));
//        mEtPwd.setText("123456");
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        //点击登录
        String user = ETTool.getData(mEtUser);
        String pwd = ETTool.getData(mEtPwd);
        if (StringTool.isEmpty(user)) {
            ToastTool.showLong("请输入账号");
            return;
        }
        if (StringTool.isEmpty(pwd)) {
            ToastTool.showLong("请输入密码");
            return;
        }
        //存储账号
        SPTool.save(MyConstant.USER_NAME, user);
        //发起登录请求
        login(user, pwd);
    }

    //后台登录
    private void login(String name, String pwd) {
        Map<String, String> map = new HashMap<>();
        map.put("userAccount", name);
        map.put("userPasswd", pwd);
        String data = getGson().toJson(map);
        LogTool.d("请求参数：" + data);

        showLoading(false);
        OkGo.<String>post(UrlPath.LOGIN)
                .upJson(data)
                .execute(getCallback());
    }
    //后台登录结果
    public StringCallback getCallback() {
        return new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ToastTool.showLong("登录成功");
                dismissLoading();
                try {
                    JSONObject jsonObject = new JSONObject(response.body());
                    JSONObject data = jsonObject.optJSONObject("data");
                    if (data == null) {
                        ToastTool.showLong("未获取到登录信息");
                        return;
                    }
                    startHome(data.optString("code"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissLoading();
                ToastTool.showLong("登录失败");
            }
        };
    }

    /**
     * 进入用户首页
     */
    private void startHome(String code) {
        SPTool.save(MyConstant.AUTO_LOGIN, "0");//自动登录
        SPTool.save(MyConstant.USER_CODE, code);//存储用户代码
        LogTool.i("进入首页Code："+code);
//        Uri uri = Uri.parse(UrlPath.HOME + "?code=" + code);
//        Intent intent = new Intent(getContext(), BrowserActivity.class);
//        intent.setData(uri);
//        startActivity(intent);
//        finish();
        Intent intent = new Intent(getContext(), BrowserActivity.class);
        intent.putExtra("path",UrlPath.HOME + "?code=" + code);
        startActivity(intent);
        finish();
    }

}
