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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseFragment;

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

import butterknife.BindView;
import butterknife.ButterKnife;

public class fragment_gx extends LinearLayout {
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.editsp)
    Spinner editsp;
    Map<String,Integer> editsp_map = new LinkedHashMap<>();
    @BindView(R.id.edittext)
    EditText edittext;
    @BindView(R.id.check)
    CheckBox check;
    private View root;
    public item_gx model;
    public fragment_gx(Context context, @Nullable AttributeSet attrs,item_gx mode) {
        super(context, attrs);
        root=LayoutInflater.from(context).inflate(R.layout.fragment_item_gx, this);
        ButterKnife.bind(this, root);
        model=mode;
        Init();
    }
    public void Init()
    {
        name.setText(model.name);
        if(model.IsWL)
        {
            check.setVisibility(View.VISIBLE);
        }else
        {
            check.setVisibility(View.GONE);
        }
        check.setChecked(false);
        switch (model.datatype)
        {
            case 0://数字
                editsp.setVisibility(View.GONE);
                edittext.setVisibility(View.VISIBLE);
                edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
                edittext.setText("0".toCharArray(), 0, "0".length());
                break;
            case 1://字符串
                editsp.setVisibility(View.GONE);
                edittext.setVisibility(View.VISIBLE);
                edittext.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 2://选项
                editsp.setVisibility(View.VISIBLE);
                edittext.setVisibility(View.VISIBLE);
                if(!model.data.isEmpty() && model.data.contains("、"))
                {
                    String[] res = model.data.split("[、]");
                    int index=1;
                    for (String item: res)
                    {
                        editsp_map.put(item,index);
                        index++;
                    }
                }else
                {
                    editsp_map.put(model.data,1);
                }
                ArrayAdapter<String> adapter2 = new ArrayAdapter<>(getContext(), R.layout.spinner_text_item, new ArrayList<>(editsp_map.keySet()));
                adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
                editsp.setAdapter(adapter2);
                break;
        }
    }
    /**
     * 获取选择变量
     * @return
     */
    public String GetValues()
    {
        String ResStu="";
        switch (model.datatype)
        {
            case 0://数字
                String text= edittext.getText().toString();
                if(text.isEmpty())
                {
                    text="0";
                }
                int val=Integer.parseInt(text);
                if(model.IsWL)
                {
                    if(check.isChecked())
                    {
                        ResStu=String.valueOf(val);
                    }
                }else
                {
                    ResStu=String.valueOf(val);
                }
                break;
            case 1://字符串
                if(model.IsWL)
                {
                    if(check.isChecked())
                    {
                        ResStu=edittext.getText().toString();
                    }
                }else
                {
                    ResStu=edittext.getText().toString();
                }
                break;
            case 2://选项
                ResStu=editsp.getSelectedItem().toString();
                break;
        }
        return ResStu;
    }

    /**
     * 设置变量
     * @param value
     */
    public void SetValues(String value)
    {
        switch (model.datatype)
        {
            case 0://数字
            case 1://字符串
                edittext.setText(value);
                if(model.IsWL)
                {
                    check.setChecked(true);
                }
                break;
            case 2://选项
                Object res= editsp_map.get(value);
                if(res!=null)
                {
                    editsp.setSelection((int)res-1);
                }
                break;
        }
    }
}