package com.app.duck.dialog;

import static com.app.duck.Config.PHONE_TAG;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSON;
import com.app.duck.R;
import com.app.duck.adapter.PhoneListAdapter;
import com.app.duck.util.HxUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;

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

public class PhoneListDialog extends Dialog implements View.OnClickListener {

    private EditText phoneEt;
    private PhoneListAdapter phoneListAdapter;
    private RecyclerView recyclerView;

    private TextView submitBt;

    private TextView closeBt;

    private List<String> mList = new ArrayList<>();

    public PhoneListDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        show();

    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_phone_list);
        String d = SPUtils.getInstance().getString("phone_list");
        try {
            if (!StringUtils.isEmpty(d)) {
                mList = JSON.parseArray(d, String.class);
            }
        } catch (Exception e) {

        }


        phoneListAdapter = new PhoneListAdapter();
        phoneListAdapter.addChildClickViewIds(R.id.iv_delete);
        phoneListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.iv_delete) {
                new MaterialDialog.Builder(getContext())
                        .title(R.string.dialogConfirmTitle)
                        .content(R.string.configDeleteConfirm)
                        .canceledOnTouchOutside(false)
                        .negativeText(R.string.cancel)
                        .positiveText(R.string.done)
                        .onNegative((dialog, which) -> dialog.dismiss())
                        .onPositive((dialog, which) -> deleteConfig(position))
                        .show();
            }
        });
        phoneEt = findViewById(R.id.phone);
        submitBt = findViewById(R.id.submitBt);
        closeBt = findViewById(R.id.closeBt);
        recyclerView = findViewById(R.id.rv);
        recyclerView.setAdapter(phoneListAdapter);
        phoneListAdapter.setList(mList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
        submitBt.setOnClickListener(this);
        closeBt.setOnClickListener(this);

    }

    private void deleteConfig(int position) {
        mList.remove(position);
        SPUtils.getInstance().put("phone_list", GsonUtils.toJson(mList));
        phoneListAdapter.removeAt(position);
        HxUtils.setPhone(toPhoneUri());
        LiveEventBus.get(PHONE_TAG).post(toPhoneUri());
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.submitBt) {
            mList.add(phoneEt.getText().toString());
            SPUtils.getInstance().put("phone_list", GsonUtils.toJson(mList));
            phoneListAdapter.addData(mList.get(mList.size() - 1));
            phoneListAdapter.notifyItemChanged(mList.size() - 1);
            HxUtils.setPhone(toPhoneUri());
            LiveEventBus.get(PHONE_TAG).post(toPhoneUri());
            phoneEt.setText("");
        }
        if (id == R.id.closeBt) {
            dismiss();
        }
    }

    private String toPhoneUri() {
        StringBuilder data = new StringBuilder();
        for (int i = 0; i < mList.size(); i++) {
            String a;
            if (i == 0) {
                a = i + "-" + mList.get(i);
            } else {
                a = "_" + i + "-" + mList.get(i);
            }
            data.append(a);
        }
        return data.toString();
    }

}
