package com.sipai.jspatrol.setting;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.sipai.jspatrol.Base.ActivityCollector;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.utils.SharedPreferencesData;

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

import cn.pedant.SweetAlert.SweetAlertDialog;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edt_in_plant,edt_off_site;
    private Button btn_no,btn_ok;
    private String ip_in_plant="",ip_off_site="";
    private String androidId;
    private TextView tv_id;

    public static void actionStart(Context context){
        Intent intent = new Intent(context,SettingActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ActivityCollector.addActivity(this);
        inits();
        initView();
    }

    private void inits() {
        SharedPreferences sharedPre=getSharedPreferences("config", 0);
        if (sharedPre.getString("ip_in_plant", "")!=null&&sharedPre.getString("ip_in_plant", "").length()>0) {
            ip_in_plant = sharedPre.getString("ip_in_plant", "");
        }
        if (sharedPre.getString("ip_off_site", "")!=null&&sharedPre.getString("ip_off_site", "").length()>0) {
            ip_off_site=sharedPre.getString("ip_off_site", "");
        }
        TelephonyManager tm = (TelephonyManager) SettingActivity.this.getSystemService(TELEPHONY_SERVICE);
//        mtyb = android.os.Build.BRAND;// 手机品牌
//        mtype = android.os.Build.MODEL; // 手机型号
//                imei = tm.getDeviceId();//uuid
        androidId = Settings.System.getString(
                getContentResolver(), Settings.Secure.ANDROID_ID);//10.0以后获取不到UUID，用androidId来代表唯一性
    }

    private void initView() {
        edt_in_plant = (EditText) findViewById(R.id.moudle_edit_ip_edt_in);
        edt_off_site = (EditText) findViewById(R.id.moudle_edit_ip_edt_off);
        if (ip_in_plant.length()>0){
            edt_in_plant.setText(ip_in_plant);
        }
        if (ip_off_site.length()>0){
            edt_off_site.setText(ip_off_site);
        }
        tv_id = findViewById(R.id.moudle_setting_tv_id);
        tv_id.setText(androidId);
        btn_no = (Button) findViewById(R.id.moudle_edit_ip_btn_no);
        btn_no.setOnClickListener(this);
        btn_ok = (Button) findViewById(R.id.moudle_edit_ip_btn_ok);
        btn_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.moudle_edit_ip_btn_no:
                Intent intent = new Intent();
                setResult(RESULT_OK,intent);
                finish();
                break;
            case R.id.moudle_edit_ip_btn_ok:
                if (edt_in_plant.getText()!=null&&edt_in_plant.getText().toString().length()>5){
                    ip_in_plant = edt_in_plant.getText().toString().trim();
                }else {
                    ip_in_plant = "no";
                }
                if (edt_off_site.getText()!=null&&edt_off_site.getText().toString().length()>5){
                    ip_off_site = edt_off_site.getText().toString();
                }else {
                    ip_off_site = "no";
                }
                if (ip_in_plant.equals("no")&&ip_off_site.equals("no")){
                    noText("all");
                }else if (ip_in_plant.equals("no")&&!ip_off_site.equals("no")){
                    noText("in");
                }else if (!ip_in_plant.equals("no")&&ip_off_site.equals("no")){
                    noText("off");
                }else {
                    saveIP(ip_in_plant,ip_off_site);
                }
                break;
        }
    }

    private void saveIP(String st_in, String st_off) {
        if (st_in.contains("：")||st_off.contains("：")){
            st_in = st_in.replaceAll("：",":");
            st_off = st_off.replaceAll("：",":");
        }
        st_in = st_in.replaceAll(" ","");
        st_off = st_off.replace(" ","");
        SharedPreferencesData sharedPreferencesData = new SharedPreferencesData(this);
        Map<String,String> map = new HashMap<>();
        map.put("ip_in_plant",st_in);
        map.put("ip_off_site",st_off);
        sharedPreferencesData.saveInfo(map);
        Intent intent = new Intent();
        setResult(RESULT_OK,intent);
        finish();
    }

    private void noText(final String st) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (st.equals("all")){
                    new SweetAlertDialog(SettingActivity.this, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("提示")
                            .setContentText("检测到厂内厂外IP均未设置，请至少设置一项后点击保存")
                            .setConfirmText("确定")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                }
                            }).show();
                }else if (st.equals("in")){
                    new SweetAlertDialog(SettingActivity.this, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("提示")
                            .setContentText("检测到厂内IP未设置，是否设置？")
                            .setConfirmText("否")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                    saveIP(ip_in_plant,ip_off_site);
                                }
                            }).setCancelText("设置")
                            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                }
                            }).show();
                }else if (st.equals("off")){
                    new SweetAlertDialog(SettingActivity.this, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("提示")
                            .setContentText("检测到厂外IP未设置，是否设置？")
                            .setConfirmText("否")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                    saveIP(ip_in_plant,ip_off_site);
                                }
                            }).setCancelText("设置")
                            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                }
                            }).show();
                }
            }
        });
    }

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