package kr.co.hanwha.hifive.admin;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.LinearLayout;

import kr.co.hanwha.hifive.HFApplication;
import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 2. 13..
 */

public class TestModeDialog extends Dialog {
    private HFApplication.SERVER_TYPE mType = HFApplication.SERVER_TYPE.REAL;

    private LinearLayout mLayoutReal;
    private LinearLayout mLayoutDev;
    private LinearLayout mLayoutLocal;

    OnDialogCallback mCallback;

    public TestModeDialog(@NonNull Context context) {
        super(context);
    }

    public TestModeDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public TestModeDialog(@NonNull Context context, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(true);
        this.mCallback = callback;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_test_mode);

        mLayoutReal = (LinearLayout) findViewById(R.id.real_linearLayout);
        mLayoutDev = (LinearLayout) findViewById(R.id.dev_linearLayout);
        mLayoutLocal = (LinearLayout) findViewById(R.id.local_linearLayout);

        findViewById(R.id.close_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.confirm_textView).setOnClickListener(onClickListener);

        mLayoutReal.setOnClickListener(onClickListener);
        mLayoutDev.setOnClickListener(onClickListener);
        mLayoutLocal.setOnClickListener(onClickListener);

        setType(HFApplication.SERVER);
    }

    private void setType(HFApplication.SERVER_TYPE type) {
        mType = type;

        mLayoutReal.setSelected(type == HFApplication.SERVER_TYPE.REAL);
        mLayoutDev.setSelected(type == HFApplication.SERVER_TYPE.DEV);
        mLayoutLocal.setSelected(type == HFApplication.SERVER_TYPE.LOCAL);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.close_imageButton:
                    TestModeDialog.this.dismiss();
                    break;
                case R.id.confirm_textView:
                    HFApplication.SERVER = mType;
                    if (mCallback != null) mCallback.onConfirm();
                    TestModeDialog.this.dismiss();
                    break;
                case R.id.real_linearLayout:
                    setType(HFApplication.SERVER_TYPE.REAL);
                    break;
                case R.id.dev_linearLayout:
                    setType(HFApplication.SERVER_TYPE.DEV);
                    break;
                case R.id.local_linearLayout:
                    setType(HFApplication.SERVER_TYPE.LOCAL);
                    break;
            }
        }
    };

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {

        /**
         * 완료 - 확인
         */
        void onConfirm();

    }
}
