package com.sipai.jspatrol.Patrol.patrolList;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import androidx.annotation.Nullable;

import com.sipai.jspatrol.Base.ActivityCollector;
import com.sipai.jspatrol.Base.BaseView;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.URL.MethodURL;
import com.sipai.jspatrol.utils.SharedPreferencesData;

import java.util.HashMap;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class PatrolListActivity extends BaseView<PatrolListPresenter, PatrolListContract.View> implements View.OnClickListener {
    private String mServer = "";
    private String mServerIp = "";
    private static final int SET_IP = 121;
    private SharedPreferencesData sharedpre;
    private String mUserid;
    private String mBizid;
    private String patrolType;

    private HashMap<String,String> map = new HashMap<>();
    private HashMap<String,String> map_base = new HashMap<>();

    private SweetAlertDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_list);
        ActivityCollector.addActivity(this);
        init();
        initView();
//        MainActivity.actionStart(PatrolListActivity.this);
    }

    private void initView() {
        pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(Color.parseColor("#419DE4"));
        pDialog.setCancelable(true);
        pDialog.setCanceledOnTouchOutside(true);
    }

    private void showMsgFinish() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                pDialog.hide();
            }
        });
    }

    private void showMsgDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                initDialog("登录中...");
                pDialog.show();
            }
        });
    }

    private void initDialog(String st) {
        pDialog.setTitleText(st);
    }

    private void init() {
        sharedpre = new SharedPreferencesData(this);
        map_base = sharedpre.getAll();
        mServer = map_base.get("mServer");
        mServerIp = map_base.get("mServerIp");

    }

    @Override
    protected PatrolListPresenter getPresenter() {
        return new PatrolListPresenter();
    }

    @Override
    public PatrolListContract.View getContract() {
        return null;
    }

    private void failLogin(final String reason) {
        if (!PatrolListActivity.this.isFinishing()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new SweetAlertDialog(PatrolListActivity.this,SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("提示")
                            .setContentText(reason)
                            .setConfirmText("确定")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                }
                            }).show();
                }
            });
        }
    }

    private void showError() {
        if (!PatrolListActivity.this.isFinishing()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(PatrolListActivity.this, "登录请求失败！", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    private void showCannotNull(final EditText edt) {
        if (!PatrolListActivity.this.isFinishing()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    edt.setError("不能为空");
                }
            });
        }
    }

    private void getPatrols() {
        showMsgDialog();
        map.put("userid",mUserid);
        map.put("bizid",mBizid);
        map.put("type",patrolType);
        p.getContract().requestData(PatrolListActivity.this,map,mServer, MethodURL.GetPatrolList_Method);
    }


    private boolean judgeString(String st, final String notice) {
        if (st!=null&&!st.isEmpty()){
            return true;
        }else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(PatrolListActivity.this, notice+"不能为空", Toast.LENGTH_SHORT).show();
                }
            });
            return false;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
