package com.android.launcher3.settings.wifi;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.android.launcher3.R;
import com.google.gson.Gson;
import com.secspace.lib.common.utils.InputUtil;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.lib.common.utils.ToastUtil;

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

public class WifiWhiteListActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG="WifiWhiteListActivity";
    private LinearLayout mLlWifi;
    private Switch mSwitchWifi;
    private RecyclerView mRecycleWifiWhiteList;
    private List<WifiInfoBean> mWifiInfoList;
    WifiManager mWifiManager;
    WifiWhiteListAdapter mWifiWhiteListAdapter;
    private TextView mTvAdd;
    private TextView mTvDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_white_list);
        mWifiInfoList = new ArrayList<>();
        initView();
        initListener();
        initData();
    }

    private void initData() {
        mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        initAdapter();
        initRecycle();
    }

    @Override
    protected void onResume() {
        super.onResume();
        wifiJudge();
    }

    private void initRecycle() {
        LinearLayoutManager linearLayout = new LinearLayoutManager(this);
        mRecycleWifiWhiteList.setLayoutManager(linearLayout);
        mRecycleWifiWhiteList.setItemAnimator(new DefaultItemAnimator());
        mRecycleWifiWhiteList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
        mRecycleWifiWhiteList.setAdapter(mWifiWhiteListAdapter);
        mWifiInfoList = loadWifiListData();
        mWifiWhiteListAdapter.setList(mWifiInfoList);
    }

    private void initAdapter() {
        mWifiWhiteListAdapter = new WifiWhiteListAdapter();
        mWifiWhiteListAdapter.setItemViewId(R.layout.item_wifi_white_list);
    }

    private void wifiJudge() {
        if (isWifiOpened()) {
            mSwitchWifi.setChecked(true);
        } else {
            mSwitchWifi.setChecked(false);
        }
    }

    private void initListener() {
        mLlWifi.setOnClickListener(this);
        mTvAdd.setOnClickListener(this);
        mTvDelete.setOnClickListener(this);
    }

    private void initView() {
        mLlWifi = findViewById(R.id.ll_wifi);
        mSwitchWifi = findViewById(R.id.swich_wifi);
        mTvAdd = findViewById(R.id.tv_add);
        mTvDelete = findViewById(R.id.tv_delete);
        mRecycleWifiWhiteList = findViewById(R.id.recycle_wifi_white_list);
        mSwitchWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setWifiState(mSwitchWifi.isChecked());
            }
        });
        findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add:
                showPwdDialog(this);
                break;
            case R.id.tv_delete:
                if (mWifiWhiteListAdapter.ismIsShowCb()){
                    mWifiWhiteListAdapter.setIsShowCb(false);
                }else {
                    mWifiWhiteListAdapter.setIsShowCb(true);
                    mWifiWhiteListAdapter.notifyDataSetChanged();
                    return;
                }
                List<WifiInfoBean> deletePosition = mWifiWhiteListAdapter.getDeletePosition();
                for (int i = 0; i < deletePosition.size(); i++) {
                    if (mWifiInfoList.contains(deletePosition.get(i))) {
                        mWifiInfoList.remove(deletePosition.get(i));
                    }
                }
                Gson gson=new Gson();
                String list2Json = gson.toJson(mWifiInfoList);
                Log.i(TAG, "onClick: "+list2Json);
                Prefs.setWifiWhiteList(this,list2Json);
                WifiManagerUtils.getInstance(this).isIllegalWifi();
                mWifiWhiteListAdapter.clearDeletePosition();
                mWifiWhiteListAdapter.notifyDataSetChanged();
                break;
        }
    }

//    24:69:68:FD:F5:A8
    @Override
    protected void onStop() {
        super.onStop();

    }

    private boolean isWifiOpened() {
        return mWifiManager.isWifiEnabled();
    }

    private void setWifiState(boolean state) {
        if (mWifiManager.isWifiEnabled()!=state){
            boolean b = mWifiManager.setWifiEnabled(state);
            Log.i(TAG, "setWifiState: "+b);
        }

    }

    private List<WifiInfoBean> loadWifiListData() {
        return Gson2ListHelper.stringWifiWhite2List(this);
    }

    private void showPwdDialog(final Context context) {
        AlertDialog alertDialog;
        final EditText editMac = new EditText(this);
        final EditText editSsid = new EditText(this);
        final EditText editPwd = new EditText(this);
        editMac.setMaxLines(1);
        editSsid.setMaxLines(1);
        editPwd.setMaxLines(1);
        editMac.setHint("请输入mac");
        editSsid.setHint("请输入ssid");
        editPwd.setHint("请输入密码");
        View view = LayoutInflater.from(this).inflate(R.layout.item_ll, null);
        final LinearLayout ll = view.findViewById(R.id.item_ll);
        ll.addView(editSsid);
        ll.addView(editMac);
        ll.addView(editPwd);
        alertDialog = new AlertDialog.Builder(this)
                .setTitle("添加新的wifi管控")
                .setCancelable(false)
                .setView(view)
                .setNegativeButton(R.string.work_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(R.string.work_sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String mac = editMac.getText().toString();
                        String ssid = editSsid.getText().toString();
                        if (TextUtils.isEmpty(ssid)) {
                            ToastUtil.showShort(WifiWhiteListActivity.this, "输入不能为空请重新输入");
                            return;
                        }
                        for (int i = 0; i < mWifiInfoList.size(); i++) {
                            String ssid1 = mWifiInfoList.get(i).getSsid();
                            if ( TextUtils.equals(ssid, ssid1)) {
                                ToastUtil.showShort(WifiWhiteListActivity.this, "不能重复输入，请重新输入");
                                return;
                            }
                        }
                        WifiInfoBean wifiInfo = new WifiInfoBean();
                        wifiInfo.setMac(mac);
                        wifiInfo.setSsid(ssid);
                        wifiInfo.setPwd(TextUtils.isEmpty(editPwd.getText().toString()) ? "" : editPwd.getText().toString());
                        mWifiInfoList.add(wifiInfo);
                        mWifiWhiteListAdapter.notifyDataSetChanged();
                        Gson gson=new Gson();
                        String list2Json = gson.toJson(mWifiInfoList);
                        Prefs.setWifiWhiteList(context,list2Json);
                        InputUtil.closeSoftInput(getApplicationContext(), editSsid);
                        dialog.dismiss();
                        WifiManagerUtils.getInstance(context).isIllegalWifi();
                    }
                })
                .create();

        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

}
