package com.xplora.xpsettings.Fragment;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.xplora.xpsettings.Observer.Listener;
import com.xplora.xpsettings.R;

public class SelectFragment extends DialogFragment {
    private Context mContext = null;
    private int type = 0;
    private String title = "";
    private String content = "";

    public static SelectFragment newInstance(Context context) {
        SelectFragment fragment = new SelectFragment();
        fragment.mContext = context;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_select, container, false);
        TextView titleTv = (TextView)rootView.findViewById(R.id.mTitle);
        TextView contentTv = (TextView)rootView.findViewById(R.id.mContent);
        Button noButton = (Button)rootView.findViewById(R.id.mNoBtn);
        Button yesButton = (Button)rootView.findViewById(R.id.mYesBtn);

        titleTv.setText(title);
        contentTv.setText(content);

        yesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Listener.SelectFragmentListener listener = (Listener.SelectFragmentListener)mContext;
                listener.onSelectFragment(type);
                remove();
            }
        });
        noButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remove();
            }
        });
        return rootView;
    }

    public void setItemInfo(int type, String title,  String content) {
        this.type = type;
        this.title = title;
        this.content = content;
    }

    private void remove() {
        dismissAllowingStateLoss();
    }

    @Override
    public void onDestroy() {
        Listener.SelectFragmentListener listener = (Listener.SelectFragmentListener)mContext;
        listener.onCancelFragment();
        super.onDestroy();
    }
}