package com.sipai.jspatrol.NFC;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.sipai.jspatrol.Bean.Biz;
import com.sipai.jspatrol.R;
import com.sipai.ui.bizPopWindow.CustomListPopupWindow;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class NFCGuideActivity extends AppCompatActivity implements CustomListPopupWindow.ItemClickCallBack{
    private Button btn_read,btn_write;
    private TextView tv_right;
    CustomListPopupWindow mPopupWindow;
    private CustomListPopupWindow.ItemClickCallBack mCallBack;
    private String mCheckedUId="";
    private Context mContext;
    private String bizs;
    private List<Biz> bizList = new ArrayList<>();
    private String mBizid;
    private String mBizName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfcguide);
        mCallBack=this;
        mContext=this;
        init();
        initView();
    }

    private void init() {
        SharedPreferences sharedPreferences = getSharedPreferences("config",MODE_PRIVATE);
        bizs = sharedPreferences.getString("biz","");
        bizList = parseBiz(bizs);
        mBizName = sharedPreferences.getString("bizname","");
    }

    private void initView() {
        TextView tv_title = findViewById(R.id.moudle_back_title_tv_title);
        tv_title.setText("NFC读写");
        ImageView btn_back = findViewById(R.id.moudle_back_title_iv_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_right = (TextView) findViewById(R.id.moudle_back_title_tv_save);
        tv_right.setText(mBizName);
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow=new CustomListPopupWindow(mContext, bizList,mCheckedUId, mCallBack);
                mPopupWindow.showAsDropDown(tv_right);
            }
        });
        btn_read = findViewById(R.id.moudle_nfcguide_btn_read);
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NFCGuideActivity.this,ReadTag.class);
                startActivity(intent);
            }
        });
        btn_write = findViewById(R.id.moudle_nfcguide_btn_write);
        btn_write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NFCGuideActivity.this,WriteTag.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void callBack(Biz biz) {
        mCheckedUId=biz.getBizid();
        final String name = biz.getName();
        saveBizid(NFCGuideActivity.this,mCheckedUId,name);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_right.setText(name);
                mPopupWindow.dismiss();
            }
        });
    }

    private List<Biz> parseBiz(String result) {
        List<Biz> list = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(result);
            for (int i=0;i<array.length();i++){
                Biz item = new Biz();
                item.setId(array.getJSONObject(i).optString("id"));
                item.setBizid(array.getJSONObject(i).optString("id"));
                item.setName(array.getJSONObject(i).optString("text"));
                list.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return list;
        }
        return list;
    }

    private void saveBizid(Context context,String bizid, String name) {
        //获取SharedPreferences对象
        SharedPreferences sharedPre=context.getSharedPreferences("config", MODE_PRIVATE);
        //获取Editor对象
        SharedPreferences.Editor editor=sharedPre.edit();
        //设置参数
        editor.putString("bizid", bizid);
        editor.putString("bizname",name);
//		editor.putString("serverip", serverip);
        //提交
        editor.commit();
        mBizid=sharedPre.getString("bizid", "");
        mBizName = sharedPre.getString("bizname","");

    }

}
