using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Principal;
using System.Threading;

using StaticAppGlobals;
using Telerik.Web.UI;

public partial class CentralTicketing_Interfaces_TicketDetails : BaseUserControl //System.Web.UI.UserControl
{
    //protected IIdentity UserId = HttpContext.Current.User.Identity;
    //protected CentralUserProfile UserProfile;
    //protected CTicketsServiceClient cticketservice = new CTicketsServiceClient("WSHttpBinding_ICTicketsService1");
    protected string VirtualHostURL = System.Configuration.ConfigurationSettings.AppSettings["VirtualHostURL"];
    protected ctickets_systems_configuration SystemConfiguration;
    protected ctickets_master CTicketData;

    public event EventHandler NeedRefresh;

    public int id_ticket = 0;
    public string last_action;

    protected void Page_Load(object sender, EventArgs e)
    {
        //UserProfile = new CentralUserProfile(UserId);

        if (txt_id_ticket.Text != "")
        {
            DetailsDiv.Visible = true;
            ShowTicketDetails(Convert.ToInt32(txt_id_ticket.Text));
            //ShowTicketDetails(id_ticket);
        }
        else
        {
            //DetailsDiv.Visible = false;
        }


        if (!Page.IsPostBack)
        {
            InitButtonParameters();
        }
    }

    public void RequestRefresh()
    {
        EventArgs e = new EventArgs();
        if (NeedRefresh != null) { NeedRefresh(this, e); }
    }

    public void ShowTicketDetails(int Id_Ticket)
    {
        if (Id_Ticket != 0)
        {
            id_ticket = Id_Ticket;
            DetailsDiv.Visible = true;
            CTicketData = cticketservice.GetTicketDetails(id_ticket);
            ShowComments();
            //CommentsList.Rebind();
            SystemConfiguration = cticketservice.GetTicketSystemConfiguration((int)CTicketData.id_system);
            FillTicketDetails();
            SetupButtonParameters();
            try
            {
                if (((Telerik.Web.UI.RadPane)(Parent)).Collapsed)
                    ((Telerik.Web.UI.RadPane)(Parent)).Collapsed = false;
            }
            catch
            { }
        }
    }

    private void FillTicketDetails()
    {
        txt_id_ticket.Text = CTicketData.id_ticket.ToString();
        txt_system_status.Text = CTicketData.system_status.ToString();
        txt_id_external.Text = CTicketData.id_external.ToString();
        txt_system_type.Text = CTicketData.ticket_type.ToString();
        txt_ticket_reference.Text = Convert.ToString(CTicketData.reference);
        txt_ticket_text.Text = CTicketData.ticket_text.ToString();
        txt_ticket_type.Text = CTicketData.ticket_type.ToString();
        txt_user_open.Text = CTicketData.user_open.ToString();
        txt_date_open.Text = CTicketData.date_open.ToString();
        txt_user_lock.Text = (CTicketData.user_lock != null ? CTicketData.user_lock.ToString(): "");
        txt_date_lock.Text = CTicketData.date_lock.ToString();
    }

    private void SetupButtonParameters()
    {
        if (UserProfile.user_login == CTicketData.user_lock)
        {
            btn_assume_release.Text = "LIBERTAR";
            btn_assume_release.Enabled = true;
            btn_detail_resolve.Text = "RESOLVER";
            btn_detail_resolve.OnClientClick = "open_popup_window('" + VirtualHostURL
                + "/CentralTicketing/WorkConsole/ResolveWindow.aspx', "
                + CTicketData.id_ticket.ToString() + ", '', '', "
                + SystemConfiguration.system_resolve_width.ToString() + ", "
                + SystemConfiguration.system_resolve_height.ToString() + ");return(false);";
            btn_detail_resolve.Enabled = true;
            btn_comment.Enabled = true;
            btn_comment.OnClientClick = "open_popup_window('" + VirtualHostURL
                + "/CentralTicketing/WorkConsole/CommentWindow.aspx', "
                + CTicketData.id_ticket.ToString() + ", '', '', "
                + "650, 600);return(false);";
            btn_transfer.Enabled = true;
            btn_transfer.OnClientClick = "open_popup_window('" + VirtualHostURL
                + "/CentralTicketing/WorkConsole/TransferWindow.aspx', "
                + CTicketData.id_ticket.ToString() + ", '', '', "
                + "564, 550);return(false);";
            btn_historic_window.Enabled = true;
            btn_historic_window.OnClientClick = "open_popup_window('" + VirtualHostURL
                + "/CentralTicketing/WorkConsole/HistoricWindow.aspx', "
                + CTicketData.id_ticket.ToString() + ", '', '', "
                + "650, 495);return(false);";
        }
        else
        {
            btn_assume_release.Text = "ASSUMIR";
            btn_detail_resolve.Text = "VER DETALHES";
            btn_detail_resolve.OnClientClick = "open_popup_window('" + VirtualHostURL
                + "/CentralTicketing/WorkConsole/ViewerWindow.aspx', "
                + CTicketData.id_ticket.ToString() + ", '', '', "
                + SystemConfiguration.system_view_width.ToString() + ", "
                + SystemConfiguration.system_view_height.ToString() + ");return(false);";

            if (CTicketData.user_lock != null && CTicketData.user_lock != "")
            {
                if (((DateTime)CTicketData.date_lock).AddMinutes(cticketservice.lock_minutes) < DateTime.Now)
                {
                    btn_assume_release.Enabled = false;
                }
                else
                {
                    int company_area_test = 0;

                    if (company_area_test == CTicketData.id_company_structure_assigned || UserProfile.login == CTicketData.user_assigned)
                        btn_assume_release.Enabled = true;
                    else
                        btn_assume_release.Enabled = false;
                }
            }
            else
            { 
                int company_area_test = 0;

                if (company_area_test == CTicketData.id_company_structure_assigned || UserProfile.login == CTicketData.user_assigned)
                    btn_assume_release.Enabled = true;
                else
                    btn_assume_release.Enabled = false;
            }

            btn_detail_resolve.Enabled = true;
            btn_comment.Enabled = false;
            btn_transfer.Enabled = false;
            btn_historic_window.Enabled = true;
            btn_historic_window.OnClientClick = "open_popup_window('" + VirtualHostURL
                + "/CentralTicketing/WorkConsole/HistoricWindow.aspx', "
                + CTicketData.id_ticket.ToString() + ", '', '', "
                + "650, 495);return(false);";
        }
    }

    private void InitButtonParameters()
    {
        btn_assume_release.Text = "ASSUMIR";
        btn_detail_resolve.Text = "VER DETALHES";
        btn_assume_release.Enabled = false;
        btn_detail_resolve.Enabled = false;
        btn_comment.Enabled = false;
        btn_transfer.Enabled = false;
        btn_historic_window.Enabled = false;
    }

    protected void CommentsList_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        ShowComments();
    }
    protected void ShowComments()
    { 
        if (CTicketData != null)    
        {
            if (CTicketData.is_confidential == 1)
            {
                ArrayList ConfidentialMessage = new ArrayList();
                ctickets_comment CommentMessage = new ctickets_comment();
                CommentMessage.comment = "ATENÇÃO!! Este Ticket é Confidencial, para visualizar os comentários é favor carregar em \"Ver Detalhes\"!";
                ConfidentialMessage.Add(CommentMessage);

                CommentsList.DataSource = ConfidentialMessage;
                CommentsList.DataBind();
            }
            else
            { 
                //CommentsList.DataSource = cticketservice.GetTicketComments(txt_id_ticket.Text == "" ? 0 : (Convert.ToInt32(txt_id_ticket.Text)));
                CommentsList.DataSource = cticketservice.GetTicketComments(CTicketData.id_ticket);
                //CommentsList.DataSource = CTicketData.ctickets_comments;
                CommentsList.DataBind();
            }
        }
    }

    protected void btn_comment_Click(object sender, EventArgs e)
    {

    }

    protected void btn_detail_resolve_Click(object sender, EventArgs e)
    {

    }

    protected void btn_assume_release_Click(object sender, EventArgs e)
    {
        if (cticketservice.isTicketLocked(Convert.ToInt32(txt_id_ticket.Text), UserProfile.user_login))
        {
            cticketservice.ReleaseTicketLock(Convert.ToInt32(txt_id_ticket.Text));
            ShowTicketDetails(Convert.ToInt32(txt_id_ticket.Text));
            last_action = "Release";
        }
        else
        {
            if (cticketservice.CreateTicketLock(Convert.ToInt32(txt_id_ticket.Text), UserProfile.user_login))
            {
                ShowTicketDetails(Convert.ToInt32(txt_id_ticket.Text));
                last_action = "Assume";
            }
        }
        //Thread.Sleep(5000);
        if (NeedRefresh != null) { NeedRefresh(this, e); }
    }

    protected void btn_historic_window_Click(object sender, EventArgs e)
    {

    }
    protected void CommentsList_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        //e.Item.BorderStyle = BorderStyle.Solid;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ((Telerik.Web.UI.RadPane)(Parent)).Collapsed = true;
        DetailsDiv.Visible = false;
        id_ticket = 0;
    }


}
