package com.bonait.bnframework.modules.home.fragment.from.fragment;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SYSTEMSET;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SystemSystemsetFragment extends BaseFragment {

    @BindView(R.id.edittext1)
    EditText edittext1;
    @BindView(R.id.edittext2)
    EditText edittext2;
    @BindView(R.id.edittext3)
    EditText edittext3;
    @BindView(R.id.edittext4)
    EditText edittext4;
    @BindView(R.id.environment)
    Spinner environment;

    ArrayList<EditText>  editTextLists=new ArrayList<>();
    Map<String,Integer> material_map = new LinkedHashMap<>();
    /**
     * 系统设置参数
     */
    ArrayList<BPA_SYSTEMSET>  bpa_systemsets=new ArrayList<>();
    private Context context;
    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_system_systemset, null);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Logger.d("我的fragment创建");
        context = getContext();
        initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("我的fragment销毁");
    }

    /**
     * 当在activity设置viewPager + BottomNavigation + fragment时，
     * 为防止viewPager左滑动切换界面，与fragment左滑返回上一界面冲突引起闪退问题，
     * 必须加上此方法，禁止fragment左滑返回上一界面。
     *
     * 切记！切记！切记！否则会闪退！
     *
     * 当在fragment设置viewPager + BottomNavigation + fragment时，则不会出现这个问题。
     */
    @Override
    protected boolean canDragBack() {
        return false;
    }

    private void initData()
    {
        editTextLists.add(edittext1);
        editTextLists.add(edittext2);
        editTextLists.add(edittext3);
        editTextLists.add(edittext4);

        material_map.put("开发环境",0);
        material_map.put("测试环境",1);
        material_map.put("正式环境",2);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(material_map.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        environment.setAdapter(adapter);

        bpa_systemsets= QueryDB.GetSystemsetALL();
        for (BPA_SYSTEMSET item:bpa_systemsets) {
            switch (item.type) {
                case 0:
                    ConfigName.getInstance().Environment=item.value;
                    environment.setSelection(material_map.get(item.value));
                    break;
                case 1:
                    ConfigName.getInstance().Address=item.value;
                    edittext1.setText(item.value);
                    break;
                case 2:
                    ConfigName.getInstance().Post=Integer.parseInt(item.value);
                    edittext2.setText(item.value);
                    break;
                case 3:
                    ConfigName.getInstance().ClientId=item.value;
                    edittext3.setText(item.value);
                    break;
                case 4:
                    ConfigName.getInstance().DeviceId=item.value;
                    edittext4.setText(item.value);
                    break;
            }
        }
    }

    @OnClick({R.id.StartButton})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.StartButton://保存按钮
                ConfigName.getInstance().Address=edittext1.getText().toString();
                ConfigName.getInstance().Post=Integer.parseInt(edittext2.getText().toString());
                ConfigName.getInstance().ClientId=edittext3.getText().toString();
                ConfigName.getInstance().DeviceId=edittext4.getText().toString();

                ConfigName.getInstance().Environment=environment.getSelectedItem().toString();
                for (BPA_SYSTEMSET item:bpa_systemsets)
                {
                    QueryDB.DeleteSystemset(item.id);
                }

                for (int i = 0; i <editTextLists.size();i++){
                    BPA_SYSTEMSET set=new BPA_SYSTEMSET();
                    set.type=i+1;
                    set.value=editTextLists.get(i).getText().toString();
                    set.deviceID = ConfigName.getInstance().DeviceId;
                    set.userID = ConfigName.getInstance().user.userID;
                    QueryDB.AddSystemset(set);
                }
                BPA_SYSTEMSET set=new BPA_SYSTEMSET();
                set.type=0;
                set.value=environment.getSelectedItem().toString();
                set.deviceID = ConfigName.getInstance().DeviceId;
                set.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set);

                ToastUtils.info("保存成功");
                break;

        }
    }
}