package kr.co.icontrols.iotplatform;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import kr.co.icontrols.iotplatform.DataClass.UserInfo;
import kr.co.icontrols.iotplatform.utils.HttpApi;
import kr.co.icontrols.iotplatform.utils.SharedPrefUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static kr.co.icontrols.iotplatform.utils.CommonUtils.ErrorLogOutput;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.WarnLogOutput;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.showNotiMsg;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.DEFAULT_NOTI_ADD_DEVICE;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.DEFAULT_NOTI_ADD_MEMBER;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.DEFAULT_NOTI_DELETE_DEVICE;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.DEFAULT_NOTI_REMOVE_MEMBER;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.NOTI_ADD_DEVICE;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.NOTI_ADD_MEMBER;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.NOTI_ALL;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.NOTI_DELETE_DEVICE;
import static kr.co.icontrols.iotplatform.utils.SharedPrefUtil.NOTI_REMOVE_MEMBER;

public class AlarmSettingActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG = "AlarmSettingActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_setting);

        Switch switchAlram = (Switch)findViewById(R.id.switchAlram);
        /**
         * 알람 허용
         */
        switchAlram.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    SharedPrefUtil.putBoolean(NOTI_ALL, true);
                    findViewById(R.id.switchAlramAddDevice).setEnabled(true);
                    findViewById(R.id.switchAlramDeleteDevice).setEnabled(true);
                    findViewById(R.id.switchAlramAddMember).setEnabled(true);
                    findViewById(R.id.switchAlramRemoveMember).setEnabled(true);
                    findViewById(R.id.switchAlramAddDevice).setAlpha(1f);
                    findViewById(R.id.switchAlramDeleteDevice).setAlpha(1f);
                    findViewById(R.id.switchAlramAddMember).setAlpha(1f);
                    findViewById(R.id.switchAlramRemoveMember).setAlpha(1f);
                }
                else {
                    SharedPrefUtil.putBoolean(NOTI_ALL, false);
                    findViewById(R.id.switchAlramAddDevice).setEnabled(false);
                    findViewById(R.id.switchAlramDeleteDevice).setEnabled(false);
                    findViewById(R.id.switchAlramAddMember).setEnabled(false);
                    findViewById(R.id.switchAlramRemoveMember).setEnabled(false);
                    findViewById(R.id.switchAlramAddDevice).setAlpha(0.5f);
                    findViewById(R.id.switchAlramDeleteDevice).setAlpha(0.5f);
                    findViewById(R.id.switchAlramAddMember).setAlpha(0.5f);
                    findViewById(R.id.switchAlramRemoveMember).setAlpha(0.5f);
                }
                if(UserInfo.pushAccess != isChecked) {
                    changeProfile(isChecked);
                }
            }
        });
        switchAlram.setChecked(UserInfo.pushAccess/*SharedPrefUtil.getBoolean(NOTI_ALL, SharedPrefUtil.DEFAULT_NOTI_ALL)*/);

        if(switchAlram.isChecked()){
            findViewById(R.id.switchAlramAddDevice).setAlpha(1f);
            findViewById(R.id.switchAlramDeleteDevice).setAlpha(1f);
            findViewById(R.id.switchAlramAddMember).setAlpha(1f);
            findViewById(R.id.switchAlramRemoveMember).setAlpha(1f);
        }else{
            findViewById(R.id.switchAlramAddDevice).setAlpha(0.5f);
            findViewById(R.id.switchAlramDeleteDevice).setAlpha(0.5f);
            findViewById(R.id.switchAlramAddMember).setAlpha(0.5f);
            findViewById(R.id.switchAlramRemoveMember).setAlpha(0.5f);
        }



        /**
         * 스마트 조명추가 알람
         */
        Switch switchAlramAddDevice = (Switch)findViewById(R.id.switchAlramAddDevice);
        switchAlramAddDevice.setEnabled(switchAlramAddDevice.isChecked());
        switchAlramAddDevice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    SharedPrefUtil.putBoolean(NOTI_ADD_DEVICE, true);
                else
                    SharedPrefUtil.putBoolean(NOTI_ADD_DEVICE, false);
            }
        });
        switchAlramAddDevice.setChecked(SharedPrefUtil.getBoolean(NOTI_ADD_DEVICE, DEFAULT_NOTI_ADD_DEVICE));

        /**
         * 스마트 조명삭제 알람
         */
        Switch switchAlramDeleteDevice = (Switch)findViewById(R.id.switchAlramDeleteDevice);
        switchAlramDeleteDevice.setEnabled(switchAlramAddDevice.isChecked());
        switchAlramDeleteDevice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    SharedPrefUtil.putBoolean(NOTI_DELETE_DEVICE, true);
                else
                    SharedPrefUtil.putBoolean(NOTI_DELETE_DEVICE, false);
            }
        });
        switchAlramDeleteDevice.setChecked(SharedPrefUtil.getBoolean(NOTI_DELETE_DEVICE, DEFAULT_NOTI_DELETE_DEVICE));

        /**
         * 멤버추가 알람
         */
        Switch switchAlramAddMember = (Switch)findViewById(R.id.switchAlramAddMember);
        switchAlramAddMember.setEnabled(switchAlramAddDevice.isChecked());
        switchAlramAddMember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    SharedPrefUtil.putBoolean(NOTI_ADD_MEMBER, true);
                else
                    SharedPrefUtil.putBoolean(NOTI_ADD_MEMBER, false);
            }
        });
        switchAlramAddMember.setChecked(SharedPrefUtil.getBoolean(NOTI_ADD_MEMBER, DEFAULT_NOTI_ADD_MEMBER));

        /**
         * 멤버삭제 알람
         */
        Switch switchAlramRemoveMember = (Switch)findViewById(R.id.switchAlramRemoveMember);
        switchAlramRemoveMember.setEnabled(switchAlramAddDevice.isChecked());
        switchAlramRemoveMember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    SharedPrefUtil.putBoolean(NOTI_REMOVE_MEMBER, true);
                else
                    SharedPrefUtil.putBoolean(NOTI_REMOVE_MEMBER, false);
            }
        });
        switchAlramRemoveMember.setChecked(SharedPrefUtil.getBoolean(NOTI_REMOVE_MEMBER, DEFAULT_NOTI_REMOVE_MEMBER));
    }

    @Override
    public void onClick(View v) {
        /*
        final View fv = v;
        fv.setEnabled(false);
        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fv.setEnabled(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }
                });
            }
        }, 500);
        */

        switch (v.getId()) {
            case R.id.btnBack:
                onBackPressed();
                break;
        }
    }

    private void changeProfile(final boolean pushAccess) {
        try {
            HttpApi.changeUserInfo("", "", "", pushAccess, UserInfo.autoUpdate, "", new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onFailure)\n" + e.toString());
                    showNotiMsg(AlarmSettingActivity.this, "알람 설정을 실패하였습니다.");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.code() == HttpApi.HTTP_NO_CONTENT) {
                        WarnLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) HTTP_NO_CONTENT");
                        if(pushAccess) {
                            showNotiMsg(AlarmSettingActivity.this, "알람을 설정 하였습니다.");
                        } else {
                            showNotiMsg(AlarmSettingActivity.this, "알람을 해제 하였습니다.");
                        }
                        UserInfo.pushAccess = pushAccess;
                    } else {
                        ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) Error code : " + response.code() + "\nresponse : " + response.toString());
                        try {
                            JSONObject json_data = new JSONObject(response.body().string());
                            ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) body : " + json_data.toString());
                        } catch (JSONException e) {
                            ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) JSONException : " + e.toString());
                        }
                        showNotiMsg(AlarmSettingActivity.this, "알람 설정을 실패하였습니다.");
                    }
                }
            });
        } catch (Exception e){
            e.printStackTrace();
            ErrorLogOutput(TAG, "HttpApi.changeUserInfo(Exception)\n" + e.toString());
            showNotiMsg(AlarmSettingActivity.this, "알람 설정을 실패하였습니다.");
        }
    }
}